我有 4RadioButtons
和一个方法,我想根据所选的RadioButton
. 我怎么能做这个简单的任务?我应该将每个IsChecked
状态绑定到我的布尔值ViewModel
还是有更好的方法?
我认为,如果我有更多不同的选项,我会使用 aComboBox
并将其选定的索引绑定到我的 ViewModel 中的 int 属性。
我有 4RadioButtons
和一个方法,我想根据所选的RadioButton
. 我怎么能做这个简单的任务?我应该将每个IsChecked
状态绑定到我的布尔值ViewModel
还是有更好的方法?
我认为,如果我有更多不同的选项,我会使用 aComboBox
并将其选定的索引绑定到我的 ViewModel 中的 int 属性。
我建议为这些选项创建一个枚举:
public enum MyOptions
{
Option1,
Option2,
Option3
}
然后在 ViewModel 中创建一个属性,该属性包含来自此 Enum 的值:
public class MyViewModel
{
public MyOptions SelectedOption {get;set;} //NotifyPropertyChange() is required.
}
RadioButtons
然后使用绑定这些EnumToBoolConverter
<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumToBoolConverter}, ConverterParameter=Option1}"/>
<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumToBoolConverter}, ConverterParameter=Option2}"/>
<RadioButton IsChecked="{Binding SelectedOption, Converter={StaticResource EnumToBoolConverter}, ConverterParameter=Option3}"/>
然后,您确定switch
ViewModel 中的简单选项选择了哪个选项:
public void SomeMethod()
{
switch (SelectedOption)
{
case MyOptions.Option1:
...
case MyOptions.Option2:
...
case MyOptions.Option3:
...
}
}