WPF DataGrid - How to move keyboard focus to newly

2019-04-07 23:03发布

问题:

WPF DataGrid add a new row if we press tab on last column of last row. But after adding the new row, the focus is moved to top row of the grid. How can we make sure that the focus is moved to the first column of new row?

回答1:

You could try something like

this.SelectRowCell(this.Items.Count - 1, 0);

But I'm not sure if that will set the focus too. If not then try the following:

DataGridCell cell = this.GetCell(this.Items.Count - 1, 0);

if (cell != null)
{
    cell.Focus();
}