1. 程式人生 > 實用技巧 >WPF遞迴設定CheckBox與TextBox禁用聯動

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);
                        }
                    }
                }
            }
        }