The RadioButton control allows a user to choose one option from a set of mutually exclusive options. When displayed in a group, only one radio button in that group can be selected at a time. This makes it ideal for scenarios where a single selection is required, such as choosing a gender, a payment method, or a configuration setting.
To use a RadioButton, you typically place it within a layout control and associate it with a label. If you have multiple RadioButtons that should function as a group, ensure they share the same GroupName property. If GroupName is not explicitly set, RadioButtons within the same parent container will automatically form a group.
A simple example demonstrating two groups of RadioButtons.
<StackPanel>
<!-- Group 1: Font Style -->
<TextBlock Text="Font Style:" Margin="0,0,0,10"/>
<RadioButton Content="Normal" GroupName="FontStyleGroup" IsChecked="True"/>
<RadioButton Content="Bold" GroupName="FontStyleGroup"/>
<RadioButton Content="Italic" GroupName="FontStyleGroup"/>
<!-- Group 2: Theme Selection -->
<TextBlock Text="Theme:" Margin="0,20,0,10"/>
<RadioButton Content="Light" GroupName="ThemeGroup"/>
<RadioButton Content="Dark" GroupName="ThemeGroup" IsChecked="True"/>
</StackPanel>
How to programmatically interact with RadioButtons.
// Assuming you have RadioButtons defined in XAML with these names
// RadioButton normalStyleRadioButton = FindName("normalStyleRadioButton") as RadioButton;
// RadioButton boldStyleRadioButton = FindName("boldStyleRadioButton") as RadioButton;
// RadioButton italicStyleRadioButton = FindName("italicStyleRadioButton") as RadioButton;
// Setting a selection programmatically
// if (boldStyleRadioButton != null)
// {
// boldStyleRadioButton.IsChecked = true;
// }
// Getting the selected value
// RadioButton selectedTheme = FindName("ThemeGroup") as RadioButton; // This is not how you get selected item from a group
// You would typically iterate through the group or bind to a ViewModel
// Example of checking which radio button in a group is selected
// string selectedStyle = "";
// if (normalStyleRadioButton != null && normalStyleRadioButton.IsChecked == true) selectedStyle = "Normal";
// else if (boldStyleRadioButton != null && boldStyleRadioButton.IsChecked == true) selectedStyle = "Bold";
// else if (italicStyleRadioButton != null && italicStyleRadioButton.IsChecked == true) selectedStyle = "Italic";
// Debug.WriteLine($"Selected Font Style: {selectedStyle}");
Key properties and events to consider when working with RadioButton:
Content: The text or UI element displayed next to the radio button.GroupName: Identifies which group the radio button belongs to.IsChecked: A boolean property indicating whether the radio button is currently selected.Checked: An event that fires when the radio button becomes checked.Unchecked: An event that fires when the radio button becomes unchecked.Indeterminate: A property for three-state scenarios (though less common for standard RadioButtons).Content property clearly describes the option.GroupName effectively to ensure correct behavior.IsChecked="True" to guide the user.ComboBox or ListBox.