RadioButton Control

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.

Key Features

Basic Usage

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.

XAML Example

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>

C# Code-Behind Example

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}");
            
Note: The code-behind example demonstrates concepts. In a real application, consider using data binding and ViewModel patterns for more robust management of UI element states.

Properties and Events

Key properties and events to consider when working with RadioButton:

Best Practices

API References

RadioButton Class

UI Controls Overview