Daniil Harik
Daniil Harik

Reputation: 4719

WPF focus textbox after Window is restored/activated from minimized state

I have a simple WPF application where user is able to minimize my application window.

After user restores Window from minimized state I need to set focus to certain TextBox.

If user before minimizing Window has not changed focus, then after restoring application everything is fine.

But problem comes when user has changed focused.

My Window has Activated event handler. And the code is following:

private void Window_Activated(object sender, EventArgs e)
{
   if (isFullView)
       tbSearch.Focus();
   else
       tbSearch2.Focus();            
}

After Window is restored from minimized state, event handler gets fired, but TextBox tbSearch does not recieve focus.

I'm I doing something wrong?

Thank You!

Upvotes: 1

Views: 3920

Answers (3)

Kent Boogaart
Kent Boogaart

Reputation: 178660

Hard to say without knowing what isFullView is set to, but I can tell you that Focus() sets the logical focus, not the keyboard focus. The control will only have keyboard focus if its focus scope is the active focus scope.

Please read this article for more information on focus in WPF.

Upvotes: 1

tom
tom

Reputation: 11

you need to recognize prev. win. state. see this post with info: http://blogs.microsoft.co.il/blogs/maxim/archive/2009/12/24/daily-tip-how-to-activate-minimized-window-form.aspx.

Upvotes: 1

arconaut
arconaut

Reputation: 3285

Try making sure if the TextBox is visible by the time that handler gets called. Maybe you have some triggers or some other things that show the TextBox later than focus is being set.

Upvotes: 0

Related Questions