Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.0
Class Library
Selector Class
Selector Events
 SelectionChanged Event
Collapse All/Expand All Collapse All
This page is specific to
.NET Framework 3.0

Other versions are also available for the following:
Selector.SelectionChanged Event
Occurs when the selection of a Selector changes.

Namespace: System.Windows.Controls.Primitives
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

Visual Basic (Declaration)
Public Event SelectionChanged As SelectionChangedEventHandler
Visual Basic (Usage)
Dim instance As Selector
Dim handler As SelectionChangedEventHandler

AddHandler instance.SelectionChanged, handler
C#
public event SelectionChangedEventHandler SelectionChanged
C++
public:
event SelectionChangedEventHandler^ SelectionChanged {
    void add (SelectionChangedEventHandler^ value);
    void remove (SelectionChangedEventHandler^ value);
}
J#
/** @event */
public void add_SelectionChanged (SelectionChangedEventHandler value)

/** @event */
public void remove_SelectionChanged (SelectionChangedEventHandler value)
JScript
In JScript, you can handle the events defined by a class, but you cannot define your own.
XAML Attribute Usage
<object SelectionChanged="SelectionChangedEventHandler" .../>

Identifier Field

SelectionChangedEvent

Routing Strategy

Bubbling

Delegate

SelectionChangedEventHandler

This event occurs whenever there is a change to a selection. A selection can be changed not only by user interaction but also by binding as well as other set values.

The following example shows how to attach the SelectionChanged event to a list box control.

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>

The following example shows how to handle the SelectionChanged event.

C#
void PrintText(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem lbi = ((sender as ListBox).SelectedItem as ListBoxItem);
    tb.Text = "   You selected " + lbi.Content.ToString() + ".";
}
Visual Basic
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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker