Skip to main content
SelectionChangedEventArgs Class
 

Provides data for the SelectionChanged event.

Namespace:   System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

Inheritance Hierarchy
SystemObject
   SystemEventArgs
     System.WindowsRoutedEventArgs
      System.Windows.ControlsSelectionChangedEventArgs

Syntax
public class SelectionChangedEventArgs : RoutedEventArgs
public ref class SelectionChangedEventArgs : RoutedEventArgs
type SelectionChangedEventArgs = 
    class
        inherit RoutedEventArgs
    end
Public Class SelectionChangedEventArgs
	Inherits RoutedEventArgs
Constructors
NameDescription
System_CAPS_pubmethod SelectionChangedEventArgs

Initializes a new instance of the SelectionChangedEventArgs class.

Properties
NameDescription
System_CAPS_pubproperty

Gets a list that contains the items that were selected.

System_CAPS_pubproperty

Gets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. (Inherited from RoutedEventArgs.)

System_CAPS_pubproperty

Gets the original reporting source as determined by pure hit testing, before any possible adjustment by a parent class.(Inherited from RoutedEventArgs.)

System_CAPS_pubproperty

Gets a list that contains the items that were unselected.

System_CAPS_pubproperty

Gets or sets the associated with this RoutedEventArgs instance. (Inherited from RoutedEventArgs.)

System_CAPS_pubproperty

Gets or sets a reference to the object that raised the event. (Inherited from RoutedEventArgs.)

Methods
NameDescription
System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethod InvokeEventHandler

Performs the proper type casting to call the type-safe SelectionChangedEventHandler delegate for the SelectionChanged event. (Overrides RoutedEventArgsInvokeEventHandler.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethod OnSetSource

When overridden in a derived class, provides a notification callback entry point whenever the value of the property of an instance changes.(Inherited from RoutedEventArgs.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

Examples

The following example creates a ListBox and subscribes to the SelectionChanged event. It uses the SelectionChangedEventArgs to find the selected item in the ListBox.

<WrapPanel Width="500" Orientation="Horizontal" Name="rectanglesPanel">
  <WrapPanel.Resources>
    <Style TargetType="Rectangle">
      <Setter Property="Height" Value="20"/>
      <Setter Property="Width" Value="20"/>
      <Setter Property="Margin" Value="5"/>
    </Style>
  </WrapPanel.Resources>
</WrapPanel>

<ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended" 
      Width="265" Height="55" Background="HoneyDew" SelectionChanged="myListBox_SelectionChanged"
      ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">
</ListBox>
void myListBox_SelectionChanged(object sender, SelectionChangedEventArgs args)
{

    BrushConverter converter = new BrushConverter();

    // Show Rectangles that are the selected colors.
    foreach (string color in args.AddedItems)
    {
        if (GetRectangle(color) == null)
        {
            Rectangle aRect = new Rectangle();
            aRect.Fill = (Brush) converter.ConvertFrom(color);
            aRect.Tag = color;
            rectanglesPanel.Children.Add(aRect);
        }

    }

    // Remove the Rectangles that are the unselected colors.
    foreach (string color in args.RemovedItems)
    {
        FrameworkElement removedItem = GetRectangle(color);
        if (removedItem != null)
        {
            rectanglesPanel.Children.Remove(removedItem);
        }
    }
}

FrameworkElement GetRectangle(string color)
{
    foreach (FrameworkElement rect in rectanglesPanel.Children)
    {
        if (rect.Tag.ToString() == color)
            return rect;
    }

    return null;
}
Private Sub myListBox_SelectionChanged(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)

    Dim converter As BrushConverter = New BrushConverter()
    Dim color As String

    ' Show Rectangles that are the selected colors.
    For Each color In args.AddedItems

        If GetRectangle(color) Is Nothing Then
            Dim aRect As Rectangle = New Rectangle()
            aRect.Fill = CType(converter.ConvertFrom(color), Brush)
            aRect.Tag = color
            rectanglesPanel.Children.Add(aRect)
        End If

    Next

    ' Remove the Rectangles that are the unselected colors.
    For Each color In args.RemovedItems

        Dim removedItem As FrameworkElement = GetRectangle(color)
        If Not removedItem Is Nothing Then
            rectanglesPanel.Children.Remove(removedItem)
        End If

    Next

End Sub

Private Function GetRectangle(ByVal color As String) As FrameworkElement
    Dim rect As FrameworkElement
    For Each rect In rectanglesPanel.Children
        If rect.Tag.ToString() = color Then
            Return rect
        End If
    Next

    Return Nothing
End Function
Version Information
.NET Framework
Available since 3.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top