語言

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」的 邊框 元素的背景顏色。

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

備註

欲了解更多資訊、設計指引及程式碼範例,請參閱 單選按鈕。

處理 SelectionChanged 事件,當選擇選項時採取行動。

你可以從控制項的 SelectItem 屬性或 SelectionChangedEventArgs.AddedItems 屬性取得所選項目。 如果你使用 event args,索引 0 只會有一個選取的項目,所以你可以這樣取得選取的項目: string colorName = e.AddedItems[0] as string;。

適用於

另請參閱