Share via


RadioButtons.SelectionChanged Event

Definition

Occurs when the currently selected item changes.

// 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;
function onSelectionChanged(eventArgs) { /* Your code */ }
radioButtons.addEventListener("selectionchanged", onSelectionChanged);
radioButtons.removeEventListener("selectionchanged", onSelectionChanged);
- or -
radioButtons.onselectionchanged = onSelectionChanged;
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".

<RadioButtons Header="Background color"
                   SelectionChanged="BackgroundColor_SelectionChanged">
    <x:String>Red</x:String>
    <x:String>Green</x:String>
    <x:String>Blue</x:String>
</RadioButtons>

...

<Border x:Name="ExampleBorder" Width="100" Height="100"/>
private void BackgroundColor_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ExampleBorder != null && sender is 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