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

 

  

 

 

 "2">
                            
                                "80"/>
                                
                                "10"/>
                                
                            
                            "用水量" 
                                      Tag="WaterConsumption"
                                      Checked="CheckBox_Checked" 
                                      Unchecked="CheckBox_Unchecked"
                                      Style="{StaticResource CheckBox_Style}"/>
                            "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"
                                          />
                            "-" Style="{StaticResource TextBlock_Style}"/>
                            "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"                                          
                                          />
                        
        /// 
        /// 递归初始化禁用Textbox
        /// 
        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);
        }

        /// 
        /// 递归修改属性
        /// 
        /// 
        /// 
        /// 
        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);
                        }
                    }
                }
            }
        }

 

你可能感兴趣的:(WPF递归设置CheckBox与TextBox禁用联动)