在 XAML 中绑定到两个布尔属性时处理异或条件
所以我有一个我想要绑定的类:
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您无法直接使用 XAML 来完成此操作。您必须在代码隐藏/视图模型中编写代码,或者编写一个自定义 IValueConverter (可能是 MultiConverter?)来处理它。不管怎样,它都是一些代码。
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.