我怎样才能最好地处理WPF单选按钮? [英] How can I best handle WPF radio buttons?

查看:155
本文介绍了我怎样才能最好地处理WPF单选按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些单选按钮在我的XAML ...

I've got some RadioButtons in my 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>

和我能处理在Visual Basic code的单击事件。该工程...

And I can handle their click events in the Visual Basic code. This works...


    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

不过,我想改善它。这code失败......

But, I'd like to improve it. This code fails...

<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中,我得到一个蓝色的波浪下划线的命令= 属性和此提示...

In my XAML I get a blue squiggly underline on the Command= attributes and this tip...

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

在我的VB中,我得到一个绿色的波浪下划线上的选择案例线和此警告...

In my VB I get a green squiggly underline on the Select Case line and this warning...

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

更妙的是,使用枚举类型的命令具有完全的智能感知,并在案件错别字的编译错误,而不是运行时错误。如何提高呢?

Even better would be to use Enum type commands with full Intellisense and compile errors rather than runtime errors in case of typos. How can I improve this?

推荐答案

为了让命令工作,你需要设置绑定在任何您的XAML或code后面。这些命令绑定必须引用已经previously声明公共静态字段。

In order for commands to work you need to set up bindings in either your xaml or code behind. These command bindings must reference public static fields that have been previously declared.

然后在你的按钮命令属性,那么你将还需要引用这些相同的命令。

Then in your buttons Command attribute you will then need to also reference these same commands.

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

这篇关于我怎样才能最好地处理WPF单选按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆