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)
        选择案例 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 类型命令并编译错误而不是运行时错误,以防出现拼写错误。我该如何改进呢?

4

2 回答 2

18

为了使命令起作用,您需要在您的 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");
        }
    }
}
于 2008-10-31T23:09:42.417 回答
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}"/>

视图模型.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 是 true 还是 false,您可以确定单选按钮是否被选中。

于 2016-10-06T17:40:21.523 回答