WPF: Radio button

This entry is part 20 of 54 in the series Learn WPF

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.

<Window x:Class="RadioSpace.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <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>

using System.Windows;
using System.Windows.Controls;

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

        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:


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:



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