在 WPF 中获取数据网格中的多个选定行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22143202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:05:50  来源:igfitidea点击:

Get the multiple selected Row in data-grid in WPF?

c#.netwpfwpfdatagridwpftoolkit

提问by Murtaza Badshah

I want to get the multiple selection of data-grid in WPF, as my the business requirement I have a customer table in data grid which allows multiple selection and radio button (ALL, Selected, All but selected). If the selected or all but selected is clicked the I have to pull data only for those customers which are selected in the data- grid. Please advice solution to get multiple selected row of data grid.

我想在 WPF 中获得数据网格的多项选择,作为我的业务需求,我在数据网格中有一个客户表,它允许进行多项选择和单选按钮(ALL、Selected、All but selected)。如果单击选定的或所有未选定的,则我必须仅为在数据网格中选择的那些客户提取数据。请建议解决方案以获取多个选定的数据网格行。

Thanks.

谢谢。

采纳答案by Murtaza Badshah

I have got a solution for the above question,

我已经得到了上述问题的解决方案,

//CustomerDTO is the DTO class which has all the column names of Customer Table.
//dgUsers is the data grid.
List<CustomerDTO> customerList ;
for (int i = 0; i < dgUsers.SelectedItems.Count; i++)
{
customerList.Add((CustomerDTO)dgUsers.SelectedItems[i]);
}

Thanks.

谢谢。

回答by faceman

There are many stackoverflow posts concerning this problem. Following are two posts which describe how to get the selected items.

有很多关于这个问题的 stackoverflow 帖子。以下是两篇文章,描述了如何获取所选项目。

Code behind approach: DataGrid get selected rows' column values

代码隐藏方法:DataGrid 获取选定行的列值

MVVM approach: Bind to SelectedItems from DataGrid

MVVM 方法:从 DataGrid 绑定到 SelectedItems