InvokeEventHandler Method
Collapse the table of content
Expand the table of content

SelectionChangedEventArgs.InvokeEventHandler Method (Delegate, Object)


Performs the proper type casting to call the type-safe SelectionChangedEventHandler delegate for the SelectionChanged event.

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

protected override void InvokeEventHandler(
	Delegate genericHandler,
	object genericTarget


Type: System.Delegate

The handler to invoke.

Type: System.Object

The current object along the event's route.

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">
    <Style TargetType="Rectangle">
      <Setter Property="Height" Value="20"/>
      <Setter Property="Width" Value="20"/>
      <Setter Property="Margin" Value="5"/>

<ListBox Name="myListBox" HorizontalAlignment="Left" SelectionMode="Extended" 
      Width="265" Height="55" Background="HoneyDew" SelectionChanged="myListBox_SelectionChanged"
      ItemsSource="{Binding Source={StaticResource Colors}}" IsSynchronizedWithCurrentItem="true">

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;


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

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

    return null;

.NET Framework
Available since 3.0
Return to top
© 2015 Microsoft