Unable to give TextBox focus on ChildWindow.Show()

Mar 23, 2011 at 11:40 AM

Hi

I got the ChildWindow example running

http://wpftoolkit.codeplex.com/wikipage?title=ChildWindow&referringTitle=Documentation

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.
MyChildWindow.Show();          
MyTextBox.Focus();

Any idea of what I'm doing wrong?

Mar 23, 2011 at 2:06 PM
Edited Mar 23, 2011 at 2: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: http://msdn.microsoft.com/en-us/library/aa969768.aspx

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 5: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 6:46 PM
Edited Apr 4, 2011 at 7: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;