WPF element binding element for IsEnabled (but for false)

I am starting to work in WPF and it seems to me that I cannot understand.

I have CheckBoxone that I would like to disable when not selected RadioButton. My current syntax is:

<CheckBox IsEnabled="{Binding ElementName=rbBoth, Path=IsChecked}">Show all</CheckBox>

Basically, I want IsEnabled to take the opposite value than the binding expression that I am currently supplying.

How can i do this? Thank.

+3
source share
4 answers

You need to use what is called a value converter (a class that implements IValueConverter.) The following is a very simple example of such a class. (Watch out for pruning ...)

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

}

, XAML, - :

<UserControl xmlns:local="clr-namespace:MyNamespace">
    <UserControl.Resources>
        <local:NegateConverter x:Key="negate" />
    </UserControl.Resources>

    ...
    <CheckBox IsEnabled="{Binding IsChecked, ElementName=rbBoth, Converter={StaticResource negate}}"
              Content="Show all" />

</UserControl>
+18

. , .

, , , . .

+1

How about this:

Create a converter for booleans:

class BooleanValueInverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (!(parameter is IValueConverter))
        {
            if (value is bool)
                return !(bool)value;
            else
                return DependencyProperty.UnsetValue;
        }
        else
        {
            IValueConverter converter = (IValueConverter)parameter;
            if (value is bool)
            {
                bool input = !(bool)value;
                return converter.Convert(input, targetType, null, culture);
            }
            else
            {
                return DependencyProperty.UnsetValue;
            }
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

in xaml imports a namespace in which the inverter class is implemented:

xmlns:util="clr-namespace:MyApp.Utilities"

In the resources section, add a link to the inverter class:

<util:BooleanValueInverter x:Key="Inverter" />

And then just use it like this:

<TextBox Text="{Binding Path=TextProperty}" IsEnabled="{Binding SomeBoolPropertyToInvert, Converter={StaticResource Inverter}}"/>
0
source
<CheckBox>
                    <CheckBox.Style>
                        <Style TargetType="CheckBox">
                            <Setter Property="Visibility" Value="Visible" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsShowName }" Value="true">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </CheckBox.Style>
  </CheckBox>
0
source

Source: https://habr.com/ru/post/1751329/


All Articles