我的XAML中有一些RadioButton …
- <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
但是,我想改进它.此代码失败…
- <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,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
在我的XAML中,我在Command =属性上得到一个蓝色的squiggly下划线,这个提示…
- 'CommandValueSerializer' ValueSerializer cannot convert from 'System.String'.
在我的VB中,我在Select Case行上看到一个绿色的下划线,这个警告…
- Runtime errors might occur when converting 'System.Windows.Input.ICommand' to 'String'.
更好的是使用Enum类型的命令与完整的Intellisense和编译错误,而不是运行时错误,以防打印错误.如何改善这个?
为了使命令工作,您需要在xaml或后面的代码中设置绑定.这些命令绑定必须引用先前声明的公共静态字段.
然后在您的按钮Command属性中,然后您还需要引用这些相同的命令.
- <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");
- }
- }
- }