Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF, give keyboard focus to control that was previously collapsed

I am trying to give a specific control keyboard focus during a sequence of presentation changes. Say a control was in a visibility state of hidden. In a function call I can change its visibility to visible, set the keyboard focus on it with no problems. However if the control was set to a visibility of collapsed and I try to set the visibility back to visible then set focus on it, it does not take.

Is there a process I can use to allow the control to move from a collapsed state, to a visible state and take keyboard focus?

like image 753
matt ososky Avatar asked Sep 03 '25 09:09

matt ososky


1 Answers

It's probably a timing issue where the control you set to visible is not ready to accept focus yet. This can happen fairly frequently when dealing with dynamic UI changes.

You should still be able to focus your control but you'll need to queue the focus action until after the control has been properly initialized. Simply using a BeginInvoke call like shown below, should do the trick.

Dispatcher.BeginInvoke(() =>
{
    //Set focus here
});
like image 104
Patrick Klug Avatar answered Sep 04 '25 23:09

Patrick Klug