RadioButtons.SelectionChanged 事件

定義

發生於目前選取的項目變更時。

// 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 

事件類型

範例

在此範例中,會 SelectionChanged 處理 事件,以變更名為 「ExampleBorder」 之 Border 元素的背景色彩。

<!-- 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;
        }
    }
}

備註

如需詳細資訊、設計指引和程式碼範例,請參閱 單選按鈕

SelectionChanged處理選擇選項時要採取動作的事件。

您可以從控件的 SelectItem 屬性或 SelectionChangedEventArgs.AddedItems 屬性取得選取的專案。 如果您使用事件自變數,則索引 0 只會有一個選取的專案,因此您可以取得選取的專案,如下所示: string colorName = e.AddedItems[0] as string;

適用於

另請參閱