SelectionChangedEventArgs.InvokeEventHandler(Delegate, Object) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
执行适当类型的强制转换,以为 SelectionChangedEventHandler 事件调用类型安全的 SelectionChanged 委托。
protected:
override void InvokeEventHandler(Delegate ^ genericHandler, System::Object ^ genericTarget);
protected override void InvokeEventHandler (Delegate genericHandler, object genericTarget);
override this.InvokeEventHandler : Delegate * obj -> unit
Protected Overrides Sub InvokeEventHandler (genericHandler As Delegate, genericTarget As Object)
参数
- genericHandler
- Delegate
要调用的处理程序。
- genericTarget
- Object
事件路由上的当前对象。
示例
以下示例创建 ListBox 并订阅 事件 SelectionChanged 。 它使用 SelectionChangedEventArgs 在 中 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