WPF element databinding for IsEnabled (but for false)

╄→尐↘猪︶ㄣ 提交于 2019-12-04 06:59:35

You need to use what's called a value converter (a class that implements IValueConverter.) A very basic example of such a class is shown below. (Watch for clipping...)

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 to include it in your XAML you would do something like:

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

Your current syntax already serves your need. It will disable the checkbox if the radiobutton is not checked.

If you really want to invert the scenario, all you need is a Converter. Take a look at this sample.

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

How about this one:

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 the xaml import the namespace where the inverter class is implemented:

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

In the resource section add reference the inverter class:

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

And then just simply use it like this:

<TextBox Text="{Binding Path=TextProperty}" IsEnabled="{Binding SomeBoolPropertyToInvert, Converter={StaticResource Inverter}}"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!