代码之家  ›  专栏  ›  技术社区  ›  Zack Peterson

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

  •  11
  • Zack Peterson  · 技术社区  · 16 年前

    我的XAML中有一些单选按钮…

    <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>
    

    我可以在VisualBasic代码中处理它们的单击事件。这工作…

        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中,我在 命令= 属性和这个提示…

    'CommandValueSerializer' ValueSerializer cannot convert from 'System.String'.

    在我的vb中,我在 选择病例 行和此警告…

    Runtime errors might occur when converting 'System.Windows.Input.ICommand' to 'String'.

    更好的方法是使用具有完整IntelliSense的枚举类型命令并编译错误,而不是在出现拼写错误时使用运行时错误。我该如何改进?

    2 回复  |  直到 8 年前
        1
  •  18
  •   Ian Oakes    16 年前

    为了使命令工作,您需要在XAML或代码隐藏中设置绑定。这些命令绑定必须引用以前声明的公共静态字段。

    然后,在buttons命令属性中,您还需要引用这些相同的命令。

    <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");
            }
        }
    }
    
        2
  •  0
  •   Sridhar Ganapathy    8 年前

    使用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
    

    可以使用上面相同的逻辑来查看您是否检查了三个收音机中的任何一个 按钮,即选项一,选项二,选项三。但是,检查布尔集ID是否为真或假,您可以确定是否选中了单选按钮。

    推荐文章