【问题标题】:Type list of selected items in a wpf datagrid在 wpf 数据网格中键入所选项目的列表
【发布时间】:2013-02-13 11:21:31
【问题描述】:

I send the selected items to a specific command when the selection changes (each item is a class X)

我将它们作为对象,如何将其转换为列表?

我试过了:

1. IList<x>  SelectedItemsList = obj as ObservableCollection<x>;

2. IList<x>  SelectedItemsList = obj as IList<x>;

3. List<x>  SelectedItemsList = obj as List<x>;

它没有帮助。

这种类型的列表:System.Windows.Controls.SelectedItemCollection我想将它转换为我的列表:ObservableCollection&lt;x&gt;/IList&lt;x&gt;/List&lt;x&gt;(ViewModel 无法识别 wpf 的控件列表)

【问题讨论】:

    标签: wpf mvvm datagrid selecteditem


    【解决方案1】:

    SelectedItems 属性的类型是非泛型IList。您不能简单地将其转换为通用 IList<T>

    但是,您可以使用LINQ 来获取IEnumerable&lt;x&gt;List&lt;x&gt;

    using System.Linq;
    
    IList list = obj as IList;
    IEnumerable<x> SelectedItemsList = list.Cast<x>();
    // or 
    List<x> SelectedItemsList = list.Cast<x>().ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      相关资源
      最近更新 更多