WPF:获取DataGrid上已单击/选定单元格的索引

WPF:获取DataGrid上已单击/选定单元格的索引,wpf,datagrid,Wpf,Datagrid,如何获取DataGrid上单击/选定单元格的索引? 我的DataGrid列是自动生成的,我不想使用任何DataTemplate <DataGrid ItemsSource="{Binding Table, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}" AutoGenerateColumns="True"> </DataGrid> 还有其他有用的属性: x.Cur

如何获取DataGrid上单击/选定单元格的索引?
我的DataGrid列是自动生成的,我不想使用任何DataTemplate

<DataGrid ItemsSource="{Binding Table, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, IsAsync=True}"
          AutoGenerateColumns="True">
</DataGrid>

还有其他有用的属性:

x.CurrentColumn;
x.CurrentItem;
x.SelectedItem;
x.SelectedValue;

这就是我找到的解决方案,当选择单元为“cell”时,您需要循环选择的单元格,获取行和列索引。 我有一个只有textcolumn的DataGrid,还有一个datatable(来自csv文件)作为itemssource

 For Each cell As DataGridCellInfo In dataGrid1.SelectedCells

         MsgBox(cell.Column.DisplayIndex)
         MsgBox(dataGrid1.Items.IndexOf(cell.Item))
 Next

我对行索引也有同样的问题,BR1COP给出的答案是唯一适合我的答案。我没有使用循环,因为我只需要一个单元格的索引,任何选定的单元格。所以我是这样用的:

DataGridCellInfo cell = myGrid.SelectedCells[0];
int rowIndex = dividingGrid.Items.IndexOf(cell.Item);

“索引”这个词是什么意思?在中没有这样的属性,或者您想对索引执行什么操作。当您可以使用实际值时。请记住,DisplayIndex会获取单元格相对于其他单元格的显示位置,并且不会计算隐藏列数。警告一句:
x。SelectedIndex
可能是也可能不是当前关注的索引,这两个索引可能不同。所选索引更多的是逻辑选择,而不是用户实际看到的“所选”。。。默认情况下:首次单击设置焦点(高亮显示,用户认为已选中);第二次单击“设置选定项”。
DataGridCellInfo cell = myGrid.SelectedCells[0];
int rowIndex = dividingGrid.Items.IndexOf(cell.Item);