如何从WPF中的datagrid获取单元格的值? [英] How to get value of a cell from datagrid in WPF?

查看:307
本文介绍了如何从WPF中的datagrid获取单元格的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

从DataGrid中选择DataGridCell

我在WPF中有一个datagrid,其中有一些列和行。当我单击一行时,我想获得所选行的第一列。我该怎么做?我可以使用LINQ吗?
thanx

I have a datagrid in WPF with some columns and rows. when I click on a row I want to get the first column of the selected row. How can I do it? can I use LINQ for that? thanx

推荐答案

var firstSelectedCellContent = this.dataGrid.Columns[0].GetCellContent(this.dataGrid.SelectedItem);
var firstSelectedCell = firstSelectedCellContent != null ? firstSelectedCellContent.Parent as DataGridCell : null;

这样,您可以获取FrameworkDataElement,它是DataGridCell和DataGridCell本身的内容。

This way you can get FrameworkElement that is a content of a DataGridCell and a DataGridCell itself.

请注意,如果DataGrid的 EnableColumnVirtualization = True ,那么您可能会从上面的代码中获得空值。

Note that if DataGrid has EnableColumnVirtualization = True, then you might get null values from the code above.

要从数据源获取实际值,对于特定的DataGridCell,要复杂一些。没有通用的方法可以执行此操作,因为DataGridCell可以由支持数据源中的多个值(属性)构成,因此您将需要为特定的DataGridColumn处理此操作。

To get the actual value from a data source, for a specific DataGridCell, is a little more complicated. There is no general way to do this, because DataGridCell can be constituted from multiple values (properties) from the backing data source, so you are going to need to handle this for a specific DataGridColumn.

这篇关于如何从WPF中的datagrid获取单元格的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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