Selector.SelectionChanged 事件

定义

Selector 的选择更改时发生。

public:
 event System::Windows::Controls::SelectionChangedEventHandler ^ SelectionChanged;
public event System.Windows.Controls.SelectionChangedEventHandler SelectionChanged;
member this.SelectionChanged : System.Windows.Controls.SelectionChangedEventHandler 
Public Custom Event SelectionChanged As SelectionChangedEventHandler 

事件类型

示例

以下示例演示如何将 SelectionChanged 事件附加到列表框控件。

<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 事件。

void PrintText(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}
Private Sub PrintText(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)

    Dim lbsender As ListBox
    Dim li As ListBoxItem

    lbsender = CType(sender, ListBox)
    li = CType(lbsender.SelectedItem, ListBoxItem)
    tb.Text = "   You selected " & li.Content.ToString & "."
End Sub

注解

每当选定内容发生更改时,将发生此事件。 不仅可以通过用户交互更改所选内容,还可以通过绑定以及其他设置值来更改选择。

路由事件信息

标识符字段 SelectionChangedEvent
路由策略 鼓 泡
委托 SelectionChangedEventHandler

适用于