<GroupBox x:Name="groupBox" Header="Operating System" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="74" Width="280">
<StackPanel>
<RadioButton GroupName="Os" Content="Windows 7 (64-bit)" IsChecked="True"/>
<RadioButton GroupName="Os" Content="Windows 7 (32-bit)" />
</StackPanel>
</GroupBox>
I have several radio button groups in my application
我的应用程序中有几个单选按钮组
How can I access which one has been checked in the Code-Behind using C#?
如何使用C#访问Code-Behind中已检查的哪一个?
Is it absolutely necessary to use x:Name= on each RadioButton or is there a better way?
是否绝对有必要在每个RadioButton上使用x:Name =还是有更好的方法吗?
Code samples always appreciated
代码示例总是受到赞赏
1 个解决方案
#1
5
Yes! There is a better way, its called binding. Outside of binding, you are pretty much stuck (I can imagine handling all the checked events separately, and assigning to an enum, but is that really better?)
是!有一种更好的方法,称为绑定。在绑定之外,你几乎陷入困境(我可以想象分别处理所有已检查的事件,并分配给枚举,但这真的更好吗?)
For radio buttons, you would typically use an enum
to represent all the possible values:
对于单选按钮,通常使用枚举来表示所有可能的值:
public enum OsTypes
{
Windows7_32,
Windows7_64
}
And then bind each of your radio buttons to a global "selected" property on your VM. You need a ValueEqualsConverter
for this:
然后将每个单选按钮绑定到VM上的全局“选定”属性。你需要一个ValueEqualsConverter:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((bool)value) ? parameter : Binding.DoNothing;
}
And then your radio buttons look like:
然后你的单选按钮看起来像:
<RadioButton Content="Windows 7 32-bit"
IsChecked= "{Binding CurrentOs,
Converter={StaticResource ValueEqualsConverter},
ConverterParameter={x:Static local:OsTypes.Windows7_32}}"
Of course, you have a property in your VM:
当然,您的VM中有一个属性:
public OsTypes CurrentOs {get; set;}
No x:Name, complicated switch statements, or anything else. Nice, clean, and well designed. MVVM works with WPF, use it!
否x:名称,复杂的switch语句或其他任何内容。漂亮,干净,设计精良。 MVVM与WPF一起使用,使用它!
#1
5
Yes! There is a better way, its called binding. Outside of binding, you are pretty much stuck (I can imagine handling all the checked events separately, and assigning to an enum, but is that really better?)
是!有一种更好的方法,称为绑定。在绑定之外,你几乎陷入困境(我可以想象分别处理所有已检查的事件,并分配给枚举,但这真的更好吗?)
For radio buttons, you would typically use an enum
to represent all the possible values:
对于单选按钮,通常使用枚举来表示所有可能的值:
public enum OsTypes
{
Windows7_32,
Windows7_64
}
And then bind each of your radio buttons to a global "selected" property on your VM. You need a ValueEqualsConverter
for this:
然后将每个单选按钮绑定到VM上的全局“选定”属性。你需要一个ValueEqualsConverter:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((bool)value) ? parameter : Binding.DoNothing;
}
And then your radio buttons look like:
然后你的单选按钮看起来像:
<RadioButton Content="Windows 7 32-bit"
IsChecked= "{Binding CurrentOs,
Converter={StaticResource ValueEqualsConverter},
ConverterParameter={x:Static local:OsTypes.Windows7_32}}"
Of course, you have a property in your VM:
当然,您的VM中有一个属性:
public OsTypes CurrentOs {get; set;}
No x:Name, complicated switch statements, or anything else. Nice, clean, and well designed. MVVM works with WPF, use it!
否x:名称,复杂的switch语句或其他任何内容。漂亮,干净,设计精良。 MVVM与WPF一起使用,使用它!