2008-10-31 8 views
11

Mam kilka radiobuttons w moim XAML ...Jak najlepiej radzić sobie z przyciskami radiowymi 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> 

I mogę obsłużyć ich zdarzenia kliknięcia w kodzie Visual Basic. To działa ...

 
    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 

Ale, chciałbym to poprawić. Ten kod nie ...

<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 

W moim XAML otrzymuję niebieski falowane podkreślają na command = atrybutami i tej końcówki ...

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

W mojej VB uzyskać zielone faliste podkreślenie linii Select Case i to ostrzeżenie ...

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

Jeszcze lepiej byłoby używać poleceń typu Enum z pełną Intellisense i kompilować błędy zamiast błędów w czasie wykonywania w przypadku literówek. Jak mogę to poprawić?

Odpowiedz

18

Aby polecenia działały, musisz ustawić powiązania w swoim kodzie lub kodzie. Te powiązania poleceń muszą odwoływać się do publicznych pól statycznych, które zostały wcześniej zadeklarowane.

Następnie w swoich przyciskach Atrybut polecenia będzie musiał również odnosić się do tych samych poleceń.

<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

Użyłem tego, ale kiedy ładuję moją stronę, nie mogę wybrać żadnego z nich. Czy mimo to można włączyć RadioButtons? – paradisonoir

+0

Nie jestem pewien, na czym polega problem, po prostu uruchomiłem próbkę i mogłem wybrać przyciski radiowe. –

+0

Jeśli wszystkie przyciski opcji są wyłączone, to domyślam się, że powiązania poleceń nie powiodły się, er, bind. –

0

Lepsze rozwiązanie przy użyciu MVVM WPF wzorzec projektowy:

Radio Przycisk sterowania 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 

Można użyć tej samej logiki powyżej, aby sprawdzić, czy sprawdziłeś ny z trzech Radio Przyciski mianowicie. Opcja pierwsza, opcja druga, opcja trzecia. Ale sprawdzając, czy Boolean ustawiono na true lub false, możesz określić, czy przycisk opcji jest zaznaczony, czy nie.

Powiązane problemy