Search This Blog

Monday, November 11, 2013

RadioButton WPF Example

WPF > Controls > ContentControl > RadioButton

RadioButton is a button that can be selected by a user and has two states: true or false.
Note: A user can select only one item at a time within a RadioButton group by placing controls inside a parent or by setting GroupName.
XAML

<Window x:Class="WpfApplication1.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" Background="#FFF3EEEE">

    <StackPanel>
        <RadioButton GroupName="grp" Checked="RadioButton_Checked">1</RadioButton>
        <RadioButton GroupName="grp" Checked="RadioButton_Checked">2</RadioButton>
        <RadioButton GroupName="grp" Checked="RadioButton_Checked">3</RadioButton>
        <RadioButton GroupName="grp" Checked="RadioButton_Checked">4</RadioButton>
    </StackPanel>

</Window>

C# Code

private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
   ShowRB(sender);
}
void ShowRB(object sender)
{
   RadioButton li = (sender as RadioButton);
   MessageBox.Show("You clicked " + li.Content.ToString());
}