Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I best handle WPF radio buttons?

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>

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

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

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

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

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?

like image 960
Zack Peterson Avatar asked Oct 31 '08 21:10

Zack Peterson


People also ask

How do I use radio buttons in XAML?

You group RadioButton controls by putting them inside the same parent container or by setting the GroupName property on each RadioButton to the same value. A RadioButton has two states: selected or cleared. When a RadioButton is selected, its IsChecked property is true.

What control do you use to create a group of radio buttons?

You group radio buttons by drawing them inside a container such as a Panel control, a GroupBox control, or a form. All radio buttons that are added directly to a form become one group. To add separate groups, you must place them inside panels or group boxes.

What can I use instead of a radio button?

When to use a radio button. If you want the user to select more than one item then you should rather use checkboxes.

Should radio buttons be stacked?

Based on research from the Neilson Norman Group as well as the various human interface guidelines for Apple and Microsoft, radio buttons should have the circle to the left of the label, and the list should be stacked vertically.


1 Answers

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");
        }
    }
}
like image 108
Ian Oakes Avatar answered Oct 27 '22 06:10

Ian Oakes