Search code examples
c#wpf

I`m trying to turn on/off usage of GroupBox by CheckBox


C#. I´m new to WPF and I can´t understand why when I turn on/off checkBox, groupBox doesn´t answer. Like at start of program groupBox is turned off, than you press once at checkBox, groupBox is turning on. When u click checkBox again, groupBox doesn´t turn off and don´t turn on/off anymore.

I´ve seen code like that at some sites and YT videos and don´t understand why it doesn`t work with me

Also I`d like to know if checkBox.AutoCheck means something here

//GroupBox is turned off by default 

//That`s the checkBox:

private void Tea_Checked(object sender, RoutedEventArgs e)
{
     if ( Tea.IsChecked == false)
     {
        groupbox.IsEnabled = false;
     }
     else
     {
         groupbox.IsEnabled = true;
     }
}

Solution

  • You can just bind the GroupBox's IsEnabled property to the CheckBox's IsChecked property.

    <CheckBox x:Name="chkGroupBox"  Content="Enable GroupBox" />
    <GroupBox IsEnabled="{Binding ElementName=chkGroupBox, Path=IsChecked}">
        <StackPanel>
            <CheckBox Content="CheckBox 1" />
            <CheckBox Content="CheckBox 2" />
            <CheckBox Content="CheckBox 3" />
        </StackPanel>
    </GroupBox>