IsEnabled 的 WPF 元素数据绑定(但为 false)

发布于 2024-09-06 20:13:54 字数 306 浏览 1 评论 0原文

我是 WPF 的初学者,有一些事情我似乎无法弄清楚。

我有一个 CheckBox,我想在未选择 RadioButton 时禁用它。 我当前的语法是:

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

所以基本上,我希望 IsEnabled 采用与我当前提供的绑定表达式相反的值。

我该怎么做?谢谢。

I'm a starter in WPF, and there's something I can't seem to figure out.

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

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

So basically, I want IsEnabled to take the opposite value than the binding expression I'm currently supplying.

How can I do this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软糖 2024-09-13 20:13:54

您需要使用所谓的值转换器(实现 IValueConverter 的类)。下面显示了此类的一个非常基本的示例。 (注意剪裁...)

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>

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>
青春有你 2024-09-13 20:13:54
<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>
<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>
泪之魂 2024-09-13 20:13:54

这个怎么样:

为布尔值创建一个转换器:

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

在 xaml 中导入实现逆变器类的命名空间:

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

在资源部分添加引用逆变器类:

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

然后像这样简单地使用它:

<TextBox Text="{Binding Path=TextProperty}" IsEnabled="{Binding SomeBoolPropertyToInvert, Converter={StaticResource Inverter}}"/>

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}}"/>
恬淡成诗 2024-09-13 20:13:54

您当前的语法已经满足您的需要。如果未选中单选按钮,它将禁用该复选框。

如果你真的想反转场景,你只需要一个转换器。看看这个示例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文