Childwindow Content Focus

Mar 1, 2011 at 7:03 AM


How do i set focus to the first focusable item in the ChildWindows' content?

After the show, i can set focus to the ChildWindow, but then I still have to tab to get to the first item in the content.



Mar 1, 2011 at 2:45 PM

Setting focus in WPF has always been a pain.  If you are using code behind to show the window then you can just give your element a x:Name and set focus to it in code right after you open it.  If you are using MVVM, well that's a whole different issue.  I am sure if you google for WPF focus issue you will find plenty of complaining.  I will soon be adding an Opening and Opened event to the control that will give you a place to write code to perform a focus operation.

Mar 2, 2011 at 12:22 PM

Thanx Brian

Mar 2, 2011 at 12:48 PM

I am able so get the text cursor to  display in textbox by setting the focusmanager (FocusManager.FocusedElement="{Binding ElementName=txtTextbox}">), but I still have to tab once to have the child windows's content gain focus.

Mar 2, 2011 at 1:36 PM

Like I said, handling focus is a major sore spot in WPF and the WPF teams needs to do some work to improve it.  I took some time to look into this, but as you are experiencing I am having a difficult time getting a consistent result.  Hopefully whenever I get the opening/opened event in there you could use it to set focus to the textbox.  Good luck.

Mar 3, 2011 at 5:51 AM

Thanx Again

Mar 23, 2011 at 5:12 PM

I think I have a solution to this problem.  I added a new property called FocusedElement.  Just bind this to the control that needs to have focus when the ChildWindow opens.  Please download the latest source and let me know if it works for you.

Jun 2, 2011 at 5:45 AM

This works very nicely, thanks for implementing the FocusedElement property.