【问题标题】:How can I best handle WPF radio buttons?如何最好地处理 WPF 单选按钮?
【发布时间】:2008-10-31 21:18:50
【问题描述】:

我的 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) 选择案例 CType(sender, RadioButton).Name 案例“RadioButton1” '做某事 退出选择 案例“RadioButton2” '做两件事 退出选择 案例“RadioButton3” '做三件事 退出选择 结束选择 结束子

但是,我想改进它。此代码失败...

<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 中,我在 Command= 属性上得到一个蓝色波浪下划线,这个提示...

'CommandValueSerializer' ValueSerializer 无法从 'System.String' 转换。

在我的 VB 中,我在 Select Case 行上得到一个绿色波浪下划线,并且这个警告...

将“System.Windows.Input.ICommand”转换为“String”时可能会出现运行时错误。

如果出现拼写错误,最好使用带有完整 Intellisense 和编译错误的 Enum 类型命令,而不是运行时错误。我该如何改进?

【问题讨论】:

    标签: .net wpf vb.net xaml


    【解决方案1】:

    为了使命令正常工作,您需要在您的 xaml 或代码后面设置绑定。这些命令绑定必须引用先前已声明的公共静态字段。

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

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

    【讨论】:

    • 我使用了这个,但是当我加载我的页面时,我无法选择其中任何一个。是否有启用选择 RadioButtons 的功能?
    • 我不确定可能是什么问题,我刚刚运行了这个示例,并且能够选择单选按钮。
    • 如果所有单选按钮都被禁用,那么我的猜测是命令绑定失败,呃,绑定。
    【解决方案2】:

    使用 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 中的任何一个 按钮即。选项一,选项二,选项三。但是检查布尔集 id 是真还是假,您可以确定单选按钮是否被选中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 2023-03-02
      • 2018-10-10
      • 2020-01-29
      • 1970-01-01
      • 2020-10-03
      • 2012-12-09
      • 2021-08-30
      相关资源
      最近更新 更多