问题描述
我有一个带有一些单选按钮的分组框.我如何知道哪个被检查了?我正在使用 WPF 并关注 MVVM.
I have a groupbox with some radiobuttons. How do I get to know which one which is checked? I am using WPF and following MVVM.
<GroupBox Name="grpbx_pagerange" Header="Print Range">
<Grid >
<RadioButton Name="radbtn_all" Content="All Pages" GroupName="radios_page_range" IsChecked="True" />
<RadioButton x:Name="radbtn_curr" Content="Current Page" GroupName="radios_page_range" />
<RadioButton Name="radbtn_pages" Content="Page Range" GroupName="radios_page_range" />
....
</GroupBox>
现在,我能想到的一种方法是将每个 RadioButton 的 IsChecked
属性绑定到 ViewModel 中的某个属性,然后在我的 ViewModel 中执行 if..else 逻辑以找出所选的单选按钮.
Now, one way I could figure out was to bind each RadioButton's IsChecked
Property to some property in ViewModel and then do if..else sort of logic in my ViewModel to figure out the selected radiobutton.
但是还有其他优雅的方式吗?
But Is there any other elegant way?
推荐答案
您可以将 Radiobuttons 的 RadioButton.Command
绑定到 ViewModel 的命令,并发送唯一的 CommandParameter 来标识哪个按钮调用了命令处理程序中的命令.
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"/>
在命令处理程序中检查参数以识别单选按钮.
in command handler check for parameter to identify the radiobutton.
谢谢
这篇关于在 MVVM 之后从 WPF 中的组框中确定选中的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!