SelectionChangedEventArgs 类(转)

为 SelectionChanged 事件提供数据。

System.Object 
  System.EventArgs
    System.Windows.RoutedEventArgs
      System.Windows.Controls.SelectionChangedEventArgs

命名空间:  System.Windows.Controls
程序集:  PresentationFramework(在 PresentationFramework.dll 中)

public class SelectionChangedEventArgs : RoutedEventArgs

SelectionChangedEventArgs 类型公开以下成员。

显示: 
 名称说明
公共方法 SelectionChangedEventArgs 初始化 SelectionChangedEventArgs 类的新实例。
页首
显示: 
 名称说明
公共属性 AddedItems 获取包含项选择的列表。
公共属性 Handled 获取或设置一个处理。 路由事件 的操作生成的值,则移动方法。(继承自 RoutedEventArgs。)
公共属性 OriginalSource 获取原始报告源由纯 命中测试,在父类的所有可能的 Source 调整之前。 (继承自 RoutedEventArgs。)
公共属性 RemovedItems 获取包含项目是未选择的列表。
公共属性 RoutedEvent 获取或设置 RoutedEvent 与此 RoutedEventArgs 实例。 (继承自 RoutedEventArgs。)
公共属性 Source 获取或设置引用引发事件的对象。 (继承自RoutedEventArgs。)
页首
显示: 
 名称说明
公共方法 Equals(Object) 确定指定的对象是否等于当前对象。 (继承自 Object。)
受保护的方法 Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
公共方法 GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)
公共方法 GetType 获取当前实例的 Type (继承自 Object。)
受保护的方法 InvokeEventHandler 执行适当的类型强制转换通知 SelectionChanged 事件的类型安全的 SelectionChangedEventHandler 委托。 (重写RoutedEventArgs.InvokeEventHandler(Delegate, Object)。)
受保护的方法 MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
受保护的方法 OnSetSource 当重写在派生类,提供通知回调入口点,只要实例的Source 属性的值更改。 (继承自 RoutedEventArgs。)
公共方法 ToString 返回表示当前对象的字符串。 (继承自 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;
}
 

posted @ 2013-10-21 13:46  邹邹  Views(1919)  Comments(0)    收藏  举报