0

I have 3 CheckBoxes and I want to disable/enable two of them when the other one is checked/unchecked. The first step works (if I check checkBoxcaja, the other two checkBoxes are disabled well, but when I uncheck checkBoxcaja, the other two checkBoxes don't return to their original Enabled state.

What am I doing wrong?

I'm a newbie in WPF.

This is the code:

private void checkBoxcaja_Checked(object sender, RoutedEventArgs e)
{
    if (checkBoxcaja.IsChecked == true)
    {
        checkBoxbanderola.IsEnabled = false;
        checkBoxletra.IsEnabled = false;
    }
    else if (checkBoxcaja.IsChecked == false)
    {
        checkBoxbanderola.IsEnabled = true;
        checkBoxletra.IsEnabled = true;
    }
}

Thanks in advance

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Fidel
  • 19
  • 4
  • One thing you could have tried is setting a breakpoint inside the event to see what was happening - the first thing you may have noticed was that the breakpoint was only ever hit when the box was checked. That may have given a clue as to direction to start looking for a solution. – PaulF Aug 06 '18 at 09:56

2 Answers2

2

I'm taking a guess here and say you connected this to the Checked event.

Well, that's only fired if the box is checked. There is an Unchecked event that is fired when it is unchecked so you need to connect to that as well. Half you method belongs there.

private void checkBoxcaja_Checked(object sender, RoutedEventArgs e)
{
    checkBoxbanderola.IsEnabled = false;
    checkBoxletra.IsEnabled = false;
}

private void checkBoxcaja_Unchecked(object sender, RoutedEventArgs e)
{
    checkBoxbanderola.IsEnabled = true;
    checkBoxletra.IsEnabled = true;
}

Don't forget to hook them up. Alternatively, you can use the same event handler for both.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • 1
    If you use the same event handler for both, you can simply do: "checkBoxbanderola.IsEnabled = !checkBoxcaja.IsChecked" etc. – Polyfun Aug 06 '18 at 10:16
0

You can also do this completely in XAML with Datatriggers:

<CheckBox x:Name="CBOne" Grid.Column="1" Grid.Row="3" Content="One" HorizontalAlignment="Center" VerticalAlignment="Center"></CheckBox>
<CheckBox x:Name="CBTwo" Grid.Column="2" Grid.Row="3" Content="Two" HorizontalAlignment="Left" VerticalAlignment="Center">
        <CheckBox.Style>
            <Style TargetType="CheckBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=CBOne, Path=IsChecked}" Value="True">
                        <Setter Property="IsEnabled" Value="False"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=CBOne, Path=IsChecked}" Value="False">
                        <Setter Property="IsEnabled" Value="True"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </CheckBox.Style>
</CheckBox>
<CheckBox x:Name="CBThree" Grid.Column="2" Grid.Row="3" Content="Three" HorizontalAlignment="Right" VerticalAlignment="Center">
        <CheckBox.Style>
            <Style TargetType="CheckBox">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=CBOne, Path=IsChecked}" Value="True">
                        <Setter Property="IsEnabled" Value="False"></Setter>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=CBOne, Path=IsChecked}" Value="False">
                        <Setter Property="IsEnabled" Value="True"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </CheckBox.Style>
</CheckBox>
Kevin Cook
  • 1,922
  • 1
  • 15
  • 16