我的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
但是,我想改进它。这段代码失败了......
<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
在我的XAML中,我在 Command = 属性上得到了一个蓝色波浪形下划线,这个提示......
'CommandValueSerializer' ValueSerializer cannot convert from 'System.String'.
在我的VB中,我在选择案例行上出现了绿色波浪形下划线并且此警告......
Runtime errors might occur when converting 'System.Windows.Input.ICommand' to 'String'.
更好的方法是使用带有完整Intellisense的Enum类型命令,并在出现拼写错误时编译错误而不是运行时错误。我怎样才能改善这个?
答案 0 :(得分:18)
为了使命令起作用,您需要在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");
}
}
}
答案 1 :(得分:0)
使用WPF MVVM设计模式的更好解决方案:
单选按钮控制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:
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
可以使用上面相同的逻辑来查看您是否检查了三个Radio中的任何一个 按钮即。选项一,选项二,选项三。但是,如果检查布尔值是否为true,则可以确定是否选中了单选按钮。