WPF: Radio button

Radio button allows only one option to be selected.  We would usually group them using GroupName.

Whether a RadioButton is selected is determined by the state of its IsChecked property.

MainWindow.xaml
<Window x:Class="RadioSpace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Label FontSize="24">How do you find codecrawl?</Label>
        <RadioButton GroupName="codecrawl" Name="rb1" Checked="DisplayOutput">Good</RadioButton>
        <RadioButton GroupName="codecrawl" Name="rb2" Checked="DisplayOutput" >Great</RadioButton>
        <RadioButton GroupName="codecrawl" Name="rb3" Checked="DisplayOutput">Just OK</RadioButton>
        <TextBlock Name="txtb" Text="Selected Button" FontSize="24" Margin="50,50,50,50"></TextBlock>
    </StackPanel>
</Window>

MainWindow.xaml.cs
using System.Windows;
using System.Windows.Controls;

namespace RadioSpace
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void DisplayOutput(object sender, RoutedEventArgs e)
        {
            RadioButton rb = (sender as RadioButton);
            txtb.Text = rb.Content.ToString();

          //txtb.Text = rb.IsChecked.ToString();
        }
    }
}

The output from running the above code is:

radiobutton

If you have like to check the state of the button, you can use this line:

txtb.Text = rb.IsChecked.ToString();

The output will be:

ischecked

 

Series Navigation<< WPF: Submitting textbox content
WPF: Selecting An Item In Listbox >>