Selector.SelectionChanged 事件

定義

發生於 Selector 的選取範圍變更時。

C#
public event System.Windows.Controls.SelectionChangedEventHandler SelectionChanged;

事件類型

範例

下列範例示範如何將事件附加 SelectionChanged 至清單方塊控制項。

XAML
<TextBox Name="tb" Width="140" Height="30"></TextBox>
<ListBox Name="lb" Width="100" Height="55" SelectionChanged="PrintText" SelectionMode="Single">
  <ListBoxItem>Item 1</ListBoxItem>
  <ListBoxItem>Item 2</ListBoxItem>
  <ListBoxItem>Item 3</ListBoxItem>
  <ListBoxItem>Item 4</ListBoxItem>
  <ListBoxItem>Item 5</ListBoxItem>
  <ListBoxItem>Item 6</ListBoxItem>
  <ListBoxItem>Item 7</ListBoxItem>
  <ListBoxItem>Item 8</ListBoxItem>
  <ListBoxItem>Item 9</ListBoxItem>
  <ListBoxItem>Item 10</ListBoxItem>
</ListBox>

下列範例示範如何處理 SelectionChanged 事件。

C#
void PrintText(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}

備註

每當選取範圍變更時,就會發生此事件。 選取範圍不僅可以透過使用者互動來變更,也可以透過系結和其他設定值來變更。

路由事件資訊

識別碼欄位 SelectionChangedEvent
路由策略 鼓 泡
代理人 SelectionChangedEventHandler

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9