我的 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>
我可以在 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)
选择案例 CType(sender, RadioButton).Command
案例“一”
'做某事
退出选择
案例“二”
'做两件事
退出选择
案例“三”
'做三件事
退出选择
结束选择
结束子
在我的 XAML 中, 上有一条蓝色波浪下划线。命令= 属性和这个提示...
“CommandValueSerializer”ValueSerializer 无法从“System.String”转换。
在我的 VB 中, 上有一条绿色波浪下划线。选择案例线和这个警告......
将“System.Windows.Input.ICommand”转换为“String”时可能会出现运行时错误。
更好的是使用带有完整 Intellisense 的 Enum 类型命令并编译错误而不是运行时错误,以防出现拼写错误。我该如何改进呢?
最佳答案
为了使命令起作用,您需要在您的 xaml 或代码后面设置绑定(bind)。这些命令绑定(bind)必须引用先前已声明的公共(public)静态字段。
然后在您的按钮命令属性中,您还需要引用这些相同的命令。
<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");
}
}
}
关于.net - 如何最好地处理 WPF 单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/254992/