2008-10-31 30 views
11

我有一些單選按鈕在我的XAML ...如何最好地處理WPF單選按鈕?

<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" 
       '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'.

更好的辦法是將Enum類型的命令與完整的Intellisense一起使用,並在出現錯誤時編譯錯誤而不是運行時錯誤。我該如何改進?

回答

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

我用過這個,但是當我加載我的頁面時,我無法選擇它們中的任何一個。無論如何可以選擇RadioButtons嗎? – paradisonoir 2009-07-15 15:51:02

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,可以確定單選按鈕是否被選中。