.netwpfdatagridfocusfocusmanager

give focus to datagrid after button is clicked


I have a button and a datagrid. They are located on the same window. I need to give focus to selected row after the button is clicked. Seems to be not as straightforward as I've thought.

So I've got a DataGridRow object. I call DataGridRow.Focus(), but the focus is still there. After reading this incredible topic I've tried using FocusManager to loose focus on the button, also tried settings Button.IsFocusable = false and then setting to true back again. Also tried giving focus to my row using this:

var scope = FocusManager.GetFocusScope(MyDataGrid);
FocusManager.SetFocusedElement(scope, row);

But it didn't help. Probably I do not understand how FocusManager works well enough. Could someone give me a clue how to give focus to a row?


Solution

  • row.Focusable = true;
    row.Focus();