Frage

How to get the row index of a DataGrid row in WPF that has the keyboard or mouse focus in it. I don't want DataGrid.SelectedIndex or DataGrid.SelectedItem, Because that returns only -1 when the cell is in edit mode.

Thanks in advance!

War es hilfreich?

Lösung

You can use this helper method to get the row index:

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;
    }
}

Use it like this in the appropriate events:

int rowIndex = DataGridHelper.GetRowIndex(yourDataGrid, yourDataGrid.SelectedCells[0]);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top