RadioButtons.SelectionChanged 事件

定義

發生于目前選取的專案變更時。

本檔適用于 Windows 應用程式 SDK 中適用于 WinUI 的 WinUI 2 for UWP (,請參閱Windows 應用程式 SDK命名空間) 。

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

事件類型

範例

在此範例中 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;

適用於

另請參閱