In this way, I tied the availability of one checkbox to the IsChecked state of another: <CheckBox IsEnabled="{Binding ElementName=myComboBox, Path=IsChecked}"></CheckBox> But I need to do the opposite so that the first checkbox becomes available when the second state has IsChecked = False. How is this done in WPF?

    2 answers 2

    You can use a value converter (a class that implements IValueConverter.) A small example:

     public class NegateConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) { if ( value is bool ) { return !(bool)value; } return value; } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) { if ( value is bool ) { return !(bool)value; } return value; } } 

    Then add it to your XAML like so:

     <UserControl xmlns:local="clr-namespace:MyNamespace"> <UserControl.Resources> <local:NegateConverter x:Key="negate" /> </UserControl.Resources> ... <CheckBox IsEnabled="{Binding IsChecked, ElementName=myComboBox, Converter={StaticResource negate}}" Content="Show all" /> </UserControl> 

    The question is transferred from here .

    • one
      Thank. I read about the converter, but I thought that it was designed for more “complex” bindings than mine. - Pavel

    I would suggest to solve this problem with the help of triggers.

     <CheckBox Grid.Row="0" Name="myCheckBox"/> <CheckBox Grid.Row="1"> <CheckBox.Style> <Style TargetType="CheckBox"> <Style.Triggers> <DataTrigger Binding="{Binding IsChecked, ElementName=myCheckBox}" Value="True"> <Setter Property="IsEnabled" Value="False"/> </DataTrigger> </Style.Triggers> </Style> </CheckBox.Style> </CheckBox>