This project has moved and is read-only. For the latest updates, please go here.

Unable to give TextBox focus on ChildWindow.Show()

Mar 23, 2011 at 12:40 PM


I got the ChildWindow example running

But How can I make sure that 

 <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding FirstName}" Margin="2" VerticalAlignment="Center" />
Has focus when the ChildWindow Show i called? 
I have added a name to the TextBox and ChildWindow

This shows the ChildWindow, but the textbox doesn't get focus.

Any idea of what I'm doing wrong?

Mar 23, 2011 at 3:06 PM
Edited Mar 23, 2011 at 3:07 PM

Focus in WPF is a real pain.  There are actually two types of focus in WPF, keyboard focus and logical focus.  You read more about it here:

Unfortunately there is no solution I know of that will consistantly give focus to a child control in the ChildWindow.  I was thinking of adding a property called FocusedElement, that could be set on the ChildWindow control that would allow me to set focus to the control when the ChildWindow appears.  I will look more into that and let you know if I make any progress.

Mar 23, 2011 at 6:10 PM

Good news.  I think I have found a solution.  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.

Apr 4, 2011 at 7:46 PM
Edited Apr 4, 2011 at 8:03 PM

Can you please give an example of how to use FocusedElement.

Nevermind I was compiling the release source instead of the main source.  It works in code:

child.FocusedElement = myTextBox;