WPF递归设置CheckBox与TextBox禁用联动

 

  

 

 

 <Grid Grid.Column="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="10"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <CheckBox Content="用水量" 
                                      Tag="WaterConsumption"
                                      Checked="CheckBox_Checked" 
                                      Unchecked="CheckBox_Unchecked"
                                      Style="{StaticResource CheckBox_Style}"/>
                            <dmcontrols:DMTextBox  Grid.Column="1"
                                          VerticalContentAlignment="Center"
                                          PreviewTextInput="DMTextBox_PreviewTextInput"
                                          KeyDown="Combo_KeyDown"
                                          input:InputMethod.IsInputMethodEnabled="False"
                                          Margin=" 10,5,10,5"
                                          Height="28"
                                          Tag="WaterConsumption"
                                          MaxLength="10"
                                          FontSize="18"
                                          />
                            <TextBlock  Text="-" Style="{StaticResource TextBlock_Style}"/>
                            <dmcontrols:DMTextBox  Grid.Column="3"
                                          VerticalContentAlignment="Center"
                                          PreviewTextInput="DMTextBox_PreviewTextInput"
                                          KeyDown="Combo_KeyDown"
                                          input:InputMethod.IsInputMethodEnabled="False"
                                          Margin=" 10,5,10,5"
                                          Height="28"
                                          Tag="WaterConsumption"
                                          MaxLength="10"
                                          FontSize="18"                                          
                                          />
                        </Grid>
        /// <summary>
        /// 递归初始化禁用Textbox
        /// </summary>
        private void SettingTextBoxIsenable(Grid grid, List<string> strList)
        {
            foreach (UIElement uiElement in grid.Children)
            {
                if (uiElement is Grid)
                {
                    SettingTextBoxIsenable((uiElement as Grid), strList);
                }
                else if (uiElement is DMTextBox)
                {

                    if (strList.Contains((uiElement as DMTextBox).Tag.ToString()))
                    {
                        (uiElement as DMTextBox).IsEnabled = false;
                        if (!(uiElement as TextBox).IsEnabled)
                        {
                            (uiElement as DMTextBox).Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A9A9A9"));
                        }
                        else
                        {
                            (uiElement as DMTextBox).Background = new SolidColorBrush(Colors.White);
                        }
                    }
                }
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {           
            EnablePriceCompositionControls((sender as CheckBox).Tag.ToString(), (bool)(sender as CheckBox).IsChecked, grid_Reading);
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            EnablePriceCompositionControls((sender as CheckBox).Tag.ToString(), (bool)(sender as CheckBox).IsChecked, grid_Reading);
        }

        /// <summary>
        /// 递归修改属性
        /// </summary>
        /// <param name="feeCode"></param>
        /// <param name="ischecked"></param>
        /// <param name="grid"></param>
        private void EnablePriceCompositionControls(string feeCode, bool ischecked, Grid grid)
        {
            foreach (UIElement uiElement in grid.Children)
            {
                if (uiElement is Grid)
                {
                    EnablePriceCompositionControls(feeCode, ischecked, (uiElement as Grid));
                }
                else if (uiElement is DMTextBox)
                {

                    if (feeCode.Equals((uiElement as DMTextBox).Tag.ToString()))
                    {
                        (uiElement as DMTextBox).IsEnabled = ischecked;
                        if (!(uiElement as TextBox).IsEnabled)
                        {
                            (uiElement as DMTextBox).Background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#A9A9A9"));
                        }
                        else
                        {
                            (uiElement as DMTextBox).Background = new SolidColorBrush(Colors.White);
                        }
                    }
                }
            }
        }

 

posted @ 2020-08-06 12:07  be--yourself  阅读(474)  评论(0编辑  收藏  举报