在XAML绑定到两个布尔属性,当处理异或条件 [英] Handling exclusive-or conditions when binding to two boolean properties in XAML

查看:243
本文介绍了在XAML绑定到两个布尔属性,当处理异或条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个类我要绑定到:

 公共类真棒{
    公共BOOL OptionA1 {搞定;组; }
    公共BOOL OptionA2 {搞定;组; }    公共BOOL OptionB1 {搞定;组; }
    公共BOOL OptionB2 {搞定;组; }
}

当我将这个类在我的GUI,我有两个单选按钮(称为radioButtonA和radioButtonB),这让A组和B之间的用户选择,我有两个复选框,让用户改变A1 / A2和B1 / B2分别取决于哪个单选器isChecked。

在类的逻辑并不A和B之间技术上区分就像我在我的GUI做的。所以,如果OptionA1 == True和OptionB1 == true,如预期的类将执行的OptionA1行为和B1。

然而,我要揭露A组和B异或:这意味着,如果OptionA1或OptionA2都设置为true,OptionB1和OptionB2都必须是假的。反之亦然。

下面绑定会自动重新规划/重新绑定取决于哪个单选/组选中该复选框,但并不强制执行组之间的异或关系。因此,用户可以点击RadioButtonA和两个复选框。如果用户随后单击RadioButtonB,OptionA1和OptionA2仍然设置为true(预期)。如何当RadioButtonB我器isChecked自动将其设置为false?

 <复选框CONTENT =DO选1>
    < CheckBox.Style>
        <风格的TargetType =复选框>
            < Style.Triggers>
                < D​​ataTrigger绑定={绑定的ElementName = radioButtonA,路径=}器isCheckedVALUE =真>
                    < setter属性=器isChecked>
                        < Setter.Value>
                            <绑定路径=OptionA1模式=双向/>
                        < /Setter.Value>
                    < /二传手>
                < / DataTrigger>
                < D​​ataTrigger绑定={绑定的ElementName = radioButtonB,路径=}器isCheckedVALUE =真>
                    < setter属性=器isChecked>
                        < Setter.Value>
                            <绑定路径=OptionB1模式=双向/>
                        < /Setter.Value>
                    < /二传手>
                < / DataTrigger>
            < /Style.Triggers>
        < /样式和GT;
    < /CheckBox.Style>
< /复选框>


解决方案

我不认为你将能够做到这一点的直XAML。你将不得不在你的code-背后/视图模型要么写code或编写自定义的IValueConverter(可能是一个多转换?)来处理它。无论哪种方式,它的一些code。

So I have a class I want to bind to:

public class Awesome {
    public bool OptionA1 { get; set; }
    public bool OptionA2 { get; set; }

    public bool OptionB1 { get; set; }
    public bool OptionB2 { get; set; }
}

When I expose this class in my GUI, I have two radio buttons (called radioButtonA and radioButtonB) which let the user pick between groups A and B, and I have two checkboxes to let the user change A1/A2 and B1/B2 respectively depending on which radiobutton ischecked.

The logic in the class doesn't technically differentiate between A and B like I do on my GUI. So if OptionA1 == True and OptionB1 == True, the class will execute behaviour for OptionA1 and B1 as expected.

However, I want to expose groups A and B as exclusive-or: meaning that if OptionA1 or OptionA2 are set to true, OptionB1 and OptionB2 must both be false. And vice versa.

The following binding will automatically repurpose/rebind the checkbox depending on which radiobutton/group is checked, but it does not enforce the exclusive-or relationship between the groups. So, the user can click RadioButtonA and check both boxes. If the user then clicks RadioButtonB, OptionA1 and OptionA2 are still set to true (as expected). How do I automatically set them to false when RadioButtonB ischecked?

<CheckBox Content="Do Option 1">
    <CheckBox.Style>
        <Style TargetType="CheckBox">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=radioButtonA, Path=IsChecked}" Value="True">
                    <Setter Property="IsChecked">
                        <Setter.Value>
                            <Binding Path="OptionA1" Mode="TwoWay"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=radioButtonB, Path=IsChecked}" Value="True">
                    <Setter Property="IsChecked">
                        <Setter.Value>
                            <Binding Path="OptionB1" Mode="TwoWay"/>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </CheckBox.Style>
</CheckBox>

解决方案

I don't think you're going to be able to do this in straight XAML. You are going to have to either write code in your code-behind/viewmodel or write a custom IValueConverter (perhaps a MultiConverter?) that handles it. Either way, its some code.

这篇关于在XAML绑定到两个布尔属性,当处理异或条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆