RadioButtons.SelectionChanged 事件

定义

当当前选定的项发生更改时发生。

本文档适用于 Windows 应用 SDKWinUI 的 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;

适用于

另请参阅