c# - 在 MVVM 之后从 WPF 中的组框确定选中的单选按钮

标签 c# wpf mvvm radio-button groupbox

我有一个带有一些单选按钮的组框。我怎么知道哪个被检查了?我正在使用 WPF 并遵循 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 属性绑定(bind)到 ViewModel 中的某个属性,然后在我的 ViewModel 中执行 if..else 类逻辑来确定所选的单选按钮。

但是还有其他优雅的方式吗?

最佳答案

您可以将 Radiobutton 的 RadioButton.Command 绑定(bind)到您的 ViewModel 的命令,并发送一个唯一的 CommandParameter 来识别哪个按钮调用了 commandhandler 中的命令。

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

在命令处理程序中检查参数以识别单选按钮。

谢谢

关于c# - 在 MVVM 之后从 WPF 中的组框确定选中的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18616721/

相关文章:

c# - WPF:单击按钮打开新窗口

c# - 创建充满点的多边形

WPF:将多个 View 绑定(bind)到 TabControl 的项目

mvvm - ViewModel 到 ViewModel 的通信

C# 将 System.Drawing.Image 附加到电子邮件

c# - IntelliTrace 独立收集器

c# - 创建一个由矩形填充的网格

c# - MVVM DDD 应用程序中的组合根

c# - Prism MVVM 结构

c# - 无焦点 ComboBoxItem