如何最好地处理WPF单选按钮?

11

我在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"
                '做某事一
                Exit Select
            Case "RadioButton2"
                '做某事二
                Exit Select
            Case "RadioButton3"
                '做某事三
                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).CommandParameter.ToString()
        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
2个回答

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");
        }
    }
}

我使用了这个,但当我加载页面时,我无法选择其中任何一个。有没有办法启用选择单选按钮? - paradisonoir
我不确定问题可能是什么,我刚刚运行了这个示例,我能够顺利选择单选按钮。 - Ian Oakes
如果所有单选按钮都被禁用了,那么我的猜测是命令绑定失败了。 - Drew Noakes

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

可以使用上述相同的逻辑来查看您是否选中了三个单选按钮之一,即选项一、选项二、选项三。但是,通过检查布尔值设置为true还是false,您可以确定单选按钮是否已选中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接