.net 我怎样才能最好地处理 WPF 单选按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/254992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I best handle WPF radio buttons?
提问by Zack Peterson
I've got some RadioButtons in my XAML...
我的 XAML 中有一些 RadioButtons ......
<StackPanel>
<RadioButton Name="RadioButton1" GroupName="Buttons" Click="ButtonsChecked" IsChecked="True">One</RadioButton>
<RadioButton Name="RadioButton2" GroupName="Buttons" Click="ButtonsChecked">Two</RadioButton>
<RadioButton Name="RadioButton3" GroupName="Buttons" Click="ButtonsChecked">Three</RadioButton>
</StackPanel>
And I can handle their click events in the Visual Basic code. This works...
我可以在 Visual Basic 代码中处理他们的点击事件。这工作...
Private Sub ButtonsChecked(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
Select Case CType(sender, RadioButton).Name
Case "RadioButton1"
'Do something one
Exit Select
Case "RadioButton2"
'Do something two
Exit Select
Case "RadioButton3"
'Do something three
Exit Select
End Select
End Sub
But, I'd like to improve it. This code fails...
但是,我想改进它。此代码失败...
<StackPanel>
<RadioButton Name="RadioButton1" GroupName="Buttons" Click="ButtonsChecked" Command="one" IsChecked="True">One</RadioButton>
<RadioButton Name="RadioButton2" GroupName="Buttons" Click="ButtonsChecked" Command="two">Two</RadioButton>
<RadioButton Name="RadioButton3" GroupName="Buttons" Click="ButtonsChecked" Command="three">Three</RadioButton>
</StackPanel>
Private Sub ButtonsChecked(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
Select Case CType(sender, RadioButton).Command
Case "one"
'Do something one
Exit Select
Case "two"
'Do something two
Exit Select
Case "three"
'Do something three
Exit Select
End Select
End Sub
In my XAML I get a blue squiggly underline on the Command=attributes and this tip...
在我的 XAML 中,我在Command=属性上有一个蓝色波浪形下划线,这个提示...
'CommandValueSerializer' ValueSerializer cannot convert from 'System.String'.
In my VB I get a green squiggly underline on the Select Caseline and this warning...
在我的 VB 中,我在Select Case行上看到一个绿色的波浪形下划线,这个警告...
Runtime errors might occur when converting 'System.Windows.Input.ICommand' to 'String'.
Even better would be to use Enum type commands with full Intellisense and compile errors rather than runtime errors in case of typos. How can I improve this?
更好的是使用具有完整 Intellisense 和编译错误的 Enum 类型命令,而不是在拼写错误的情况下运行时错误。我该如何改进?
回答by Ian Oakes
In order for commands to work you need to set up bindings in either your xaml or code behind. These command bindings must reference public static fields that have been previously declared.
为了使命令起作用,您需要在 xaml 或代码隐藏中设置绑定。这些命令绑定必须引用先前声明的公共静态字段。
Then in your buttons Command attribute you will then need to also reference these same commands.
然后在您的按钮命令属性中,您还需要引用这些相同的命令。
<Window
x:Class="RadioButtonCommandSample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RadioButtonCommandSample"
Title="Window1"
Height="300"
Width="300"
>
<Window.CommandBindings>
<CommandBinding Command="{x:Static local:Window1.CommandOne}" Executed="CommandBinding_Executed"/>
<CommandBinding Command="{x:Static local:Window1.CommandTwo}" Executed="CommandBinding_Executed"/>
<CommandBinding Command="{x:Static local:Window1.CommandThree}" Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
<StackPanel>
<RadioButton Name="RadioButton1" GroupName="Buttons" Command="{x:Static local:Window1.CommandOne}" IsChecked="True">One</RadioButton>
<RadioButton Name="RadioButton2" GroupName="Buttons" Command="{x:Static local:Window1.CommandTwo}">Two</RadioButton>
<RadioButton Name="RadioButton3" GroupName="Buttons" Command="{x:Static local:Window1.CommandThree}">Three</RadioButton>
</StackPanel>
</Window>
public partial class Window1 : Window
{
public static readonly RoutedCommand CommandOne = new RoutedCommand();
public static readonly RoutedCommand CommandTwo = new RoutedCommand();
public static readonly RoutedCommand CommandThree = new RoutedCommand();
public Window1()
{
InitializeComponent();
}
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (e.Command == CommandOne)
{
MessageBox.Show("CommandOne");
}
else if (e.Command == CommandTwo)
{
MessageBox.Show("CommandTwo");
}
else if (e.Command == CommandThree)
{
MessageBox.Show("CommandThree");
}
}
}
回答by Sridhar Ganapathy
Better Solution using WPF MVVM Design Pattern:
使用 WPF MVVM 设计模式的更好解决方案:
Radio Button Control XAML to Modelview.vb/ModelView.cs :
单选按钮控制 XAML 到 Modelview.vb/ModelView.cs :
XAML Code:
<RadioButton Content="On" IsEnabled="True" IsChecked="{Binding OnJob}"/>
<RadioButton Content="Off" IsEnabled="True" IsChecked="{Binding OffJob}"/>
ViewModel.vb :
视图模型.vb :
Private _OffJob As Boolean = False
Private _OnJob As Boolean = False
Public Property OnJob As Boolean
Get
Return _OnJob
End Get
Set(value As Boolean)
Me._OnJob = value
End Set
End Property
Public Property OffJob As Boolean
Get
Return _OffJob
End Get
Set(value As Boolean)
Me._OffJob = value
End Set
End Property
Private Sub FindCheckedItem()
If(Me.OnJob = True)
MessageBox.show("You have checked On")
End If
If(Me.OffJob = False)
MessageBox.Show("You have checked Off")
End sub
One can use the same logic above to see if you checked any of the three Radio Buttons viz. Option One, Option Two, Option Three. But checking if the Boolean set id true or false you can identify whether the radio button is checked or not.
可以使用上述相同的逻辑来查看您是否选中了三个单选按钮中的任何一个。方案一,方案二,方案三。但是检查布尔集 id 是否为真或假,您可以确定单选按钮是否被选中。

