Share via


RadioButtons.SelectionChanged Event

Definition

Occurs when the currently selected item changes.

This documentation applies to WinUI 2 for UWP (for WinUI in the Windows App SDK, see the Windows App SDK namespaces).

// Register
event_token SelectionChanged(SelectionChangedEventHandler const& handler) const;

// Revoke with event_token
void SelectionChanged(event_token const* cookie) const;

// Revoke with event_revoker
RadioButtons::SelectionChanged_revoker SelectionChanged(auto_revoke_t, SelectionChangedEventHandler const& handler) const;
public event SelectionChangedEventHandler SelectionChanged;
Public Custom Event SelectionChanged As SelectionChangedEventHandler 

Event Type

Examples

In this example, the SelectionChanged event is handled to change the background color of a Border element named "ExampleBorder".

<!-- xmlns:muxc="using:Microsoft.UI.Xaml.Controls -->
<muxc:RadioButtons Header="Background color"
                   SelectionChanged="BackgroundColor_SelectionChanged">
    <x:String>Red</x:String>
    <x:String>Green</x:String>
    <x:String>Blue</x:String>
</muxc:RadioButtons>

...

<Border x:Name="ExampleBorder" Width="100" Height="100"/>
// xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ExampleBorder != null && sender is muxc.RadioButtons rb)
    {
        string colorName = rb.SelectedItem as string;
        switch (colorName)
        {
            case "Red":
                ExampleBorder.Background = new SolidColorBrush(Colors.Red);
                break;
            case "Green":
                ExampleBorder.Background = new SolidColorBrush(Colors.Green);
                break;
            case "Blue":
                ExampleBorder.Background = new SolidColorBrush(Colors.Blue);
                break;
        }
    }
}

Remarks

For more info, design guidance, and code examples, see Radio buttons.

Handle the SelectionChanged event to take action when an option is chosen.

You can get the selected item from the control's SelectItem property or from the SelectionChangedEventArgs.AddedItems property. If you use the event args, there will only be one selected item, at index 0, so you could get the selected item like this: string colorName = e.AddedItems[0] as string;.

Applies to

See also