I\'ve got some RadioButtons in my XAML...
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");
}
}
}
Better Solution using WPF MVVM Design Pattern:
Radio Button Control XAML to 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
One can use the same logic above to see if you checked any of the three Radio Buttons viz. Option One, Option Two, Option Three. But checking if the Boolean set id true or false you can identify whether the radio button is checked or not.