SelectionChangedEventArgs 类

定义

提供事件的数据 SelectionChanged

public ref class SelectionChangedEventArgs : System::Windows::RoutedEventArgs
public class SelectionChangedEventArgs : System.Windows.RoutedEventArgs
type SelectionChangedEventArgs = class
    inherit RoutedEventArgs
Public Class SelectionChangedEventArgs
Inherits RoutedEventArgs
继承
SelectionChangedEventArgs

示例

以下示例创建 ListBox 并订阅事件 SelectionChanged 。 它使用 /a0> 查找所选项。

<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

构造函数

名称 说明
SelectionChangedEventArgs(RoutedEvent, IList, IList)

初始化 SelectionChangedEventArgs 类的新实例。

属性

名称 说明
AddedItems

获取包含选定项的列表。

Handled

获取或设置一个值,该值指示路由事件在传输路由时的事件处理的当前状态。

(继承自 RoutedEventArgs)
OriginalSource

获取由纯命中测试确定的原始报告源,然后再由父类进行任何可能的 Source 调整。

(继承自 RoutedEventArgs)
RemovedItems

获取一个列表,其中包含未选择的项。

RoutedEvent

获取或设置 RoutedEvent 与此 RoutedEventArgs 实例关联的项。

(继承自 RoutedEventArgs)
Source

获取或设置对引发事件的对象的引用。

(继承自 RoutedEventArgs)

方法

名称 说明
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
InvokeEventHandler(Delegate, Object)

执行正确的类型强制转换以调用事件的类型安全 SelectionChangedEventHandler 委托 SelectionChanged

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
OnSetSource(Object)

在派生类中重写时,每当实例属性的值 Source 发生更改时,都提供通知回调入口点。

(继承自 RoutedEventArgs)
ToString()

返回一个表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅