WPF DataGrid:获取单元格行索引的有效方法

时间:2018-05-05 08:45:08

标签: wpf datagrid

网络上有数以万计的问题,包括StackOverflow(例如WPF DataGrid: How to Determine the Current Row Index?WPF Datagrid cell, cellinfo and selectedcells + custom selection)。我对这些问题的答案的问题是,他们需要在项目中进行线性搜索,这是无效的。

我注意到DataGridCellInfo有一个名为internal的{​​{1}}属性,后者又有一个名为ItemInfo的属性。这Index正是我想要的。为什么此属性设计为Index?如何正确访问它?

请注意,我希望与public合作,因此SelectionUnit="Cell"始终等于SelectedIndex

1 个答案:

答案 0 :(得分:0)

也许我在这里遗漏了一些东西 如果您有一个单元格的datagridcellinfo。

myDataGrid.Items.IndexOf(datagridcellinfo.Item)