0
如何获取WPF中具有键盘或鼠标焦点的DataGrid
行的行索引。我不希望DataGrid.SelectedIndex
或DataGrid.SelectedItem
,因为当单元格处于编辑模式时,它只返回-1。获取光标位于其中的WPF Datagrid行的行索引
在此先感谢!
如何获取WPF中具有键盘或鼠标焦点的DataGrid
行的行索引。我不希望DataGrid.SelectedIndex
或DataGrid.SelectedItem
,因为当单元格处于编辑模式时,它只返回-1。获取光标位于其中的WPF Datagrid行的行索引
在此先感谢!
您可以使用此helper方法获取行索引:
public static class DataGridHelper
{
static public int GetRowIndex(DataGrid dataGrid, DataGridCellInfo dataGridCellInfo)
{
DataGridRow dgrow = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(dataGridCellInfo.Item);
if (dgrow != null)
return dgrow.GetIndex();
return -1;
}
}
使用它像这样在适当的事件:
int rowIndex = DataGridHelper.GetRowIndex(yourDataGrid, yourDataGrid.SelectedCells[0]);
谢谢你这么多的Eirik!我会尽快尝试。 –
它像魅力一样工作! :-)百万感谢你 –