且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

单选按钮,绑定,转换器

更新时间:2023-10-08 23:14:04

这是RadioButton的问题.一旦取消检查,它将失去其绑定.要解决此问题,可以将Radiobuttons的RadioButton.Command绑定到ViewModel的命令,并发送唯一的CommandParameter来标识哪个按钮在commandhandler中调用了该命令.

This is the problem with RadioButton. It loses its binding once it gets unchecked. To fix this, you can bind RadioButton.Command of Radiobuttons to a command of your ViewModel and send a unique CommandParameter to identify which button has called the command in commandhandler.

<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/>
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/>

,并且可以在命令处理程序中根据接收到的命令参数设置属性,而不是在转换器中进行设置.

and in the command handler you can set the property depending on the command parameter received instead of doing it in converter.