Share via


RadioButtons.SelectionChanged Kejadian

Definisi

Terjadi ketika item terpilih saat ini berubah.

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

Jenis Acara

Contoh

Dalam contoh ini, peristiwa ditangani SelectionChanged untuk mengubah warna latar belakang elemen Border bernama "ExampleBorder".

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

Keterangan

Untuk informasi selengkapnya, panduan desain, dan contoh kode, lihat Tombol radio.

SelectionChanged Tangani peristiwa untuk mengambil tindakan saat opsi dipilih.

Anda bisa mendapatkan item yang dipilih dari properti SelectItem kontrol atau dari properti SelectionChangedEventArgs.AddedItems . Jika Anda menggunakan argumen peristiwa, hanya akan ada satu item yang dipilih, pada indeks 0, sehingga Anda bisa mendapatkan item yang dipilih seperti ini: string colorName = e.AddedItems[0] as string;.

Berlaku untuk

Lihat juga