问题描述
||
所以我想绑定一个类:
public class Awesome {
public bool OptionA1 { get; set; }
public bool OptionA2 { get; set; }
public bool OptionB1 { get; set; }
public bool OptionB2 { get; set; }
}
当我在GUI中公开该类时,我有两个单选按钮(称为radioButtonA和radioButtonB),它们使用户可以在组A和B之间进行选择,并且有两个复选框可以使用户分别根据需要更改A1 / A2和B1 / B2选中了哪个单选按钮。
该类中的逻辑在技术上不会像我在GUI上一样区分A和B。因此,如果OptionA1 == True和OptionB1 == True,则该类将按预期执行OptionA1和B1的行为。
但是,我要将组A和B公开为“异或”:这意味着,如果OptionA1或OptionA2设置为true,则OptionB1和OptionB2都必须为false。反之亦然。
以下绑定将根据选中了哪个单选按钮/组来自动重新使用/重新绑定复选框,但不会强制组之间的异或关系。因此,用户可以单击RadioButtonA并选中两个框。如果用户然后单击RadioButtonB,则OptionA1和OptionA2仍设置为true(按预期)。选中RadioButtonB时,如何自动将它们设置为false?
<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>
解决方法
我认为您将无法在直接XAML中做到这一点。您将不得不在代码隐藏/视图模型中编写代码,或者编写一个处理它的自定义IValueConverter(也许是MultiConverter?)。无论哪种方式,它都有一些代码。