在wpf datagrid中键入所选项目的列表 [英] Type list of selected items in a wpf datagrid

查看:130
本文介绍了在wpf datagrid中键入所选项目的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当选择更改(每个项目是X类)时,我将所选项目发送到特定命令。

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

我将它们作为对象,将其转换为一个列表?

I get them as object how can I convert it to a list?

我试过:

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< x> / IList< x> / List< ; x> (ViewModel未识别wpf的控件列表)

This type of list: System.Windows.Controls.SelectedItemCollection I want to convert it to my list: ObservableCollection<x>/IList<x>/List<x> (the ViewModel not recognized a list of controls of wpf)

推荐答案

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

The type of the SelectedItems property is the non-generic IList. You can't simply cast that to the generic IList<T>.

然而,您可以使用 LINQ 获取 IEnumerable< x> 列表< x>

You could however use LINQ to get an IEnumerable<x> or a List<x>.

using System.Linq;

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

这篇关于在wpf datagrid中键入所选项目的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆