绑定到单选按钮
我将以下单选按钮绑定到变量 IsAllowed
<RadioButton Name="YesRadioButton" Margin="5,5,0,0" IsChecked="{Binding Path=IsAllowed, Mode=TwoWay}">Yes</RadioButton>
如何使“否”按钮仅使用 XAML 获取相反的值?
I have the following radio button bound to the variable IsAllowed
<RadioButton Name="YesRadioButton" Margin="5,5,0,0" IsChecked="{Binding Path=IsAllowed, Mode=TwoWay}">Yes</RadioButton>
How can I make the No button to take the opposite value only using XAML ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用 IValueConverter 编写转换器。以下是如何执行此操作的示例WPF - 使用转换器绑定到相反的布尔值
You will have to write converter using IValueConverter. Here is an example how to do it WPF - Bind to Opposite Boolean Value Using a Converter
上面的答案有效,但我想要一个适用于“是”和“否”单选按钮并反映可为空布尔值的转换器。因此,我采用了利用转换器参数的替代方案:
XAML 如下所示:
The above answer works but I wanted a converter that would apply to both Yes and No radio buttons and reflect the value of nullable booleans. So I made the alternative that takes advantage of converter parameters:
Here is what the XAML looks like:
你不需要这样做。默认情况下会发生这种情况。
只需确保
IsAllowed
以true
开头,其余部分将自行处理。这是因为当您单击
No
按钮时,它会自动设置Yes
按钮的选中值(这就是单选按钮的工作原理),因此更改将自动发生,并且您支持类将被更新。更好:只需使用复选框即可。是/否的情况正是它们的设计目的。
You do not need to. It will happen by default.
Just make sure that
IsAllowed
starts off astrue
, and the rest will take care of its self.This is because when you click on the
No
button, it will automatically set theYes
button's checked value (that's how radio buttons work), so the change will happen automatically and you backing class will be updated.EVEN BETTER: Just use a check box. Yes/no situations are what they are designed for.
不存在仅 Xaml 的解决方案。不过,您可以使用反向布尔转换器绑定“否”。
There is no Xaml-only solution. You could bind No using a reverse bool Converter though.