tags:

views:

374

answers:

2

Hi, I would like to set the focus on a textbox in my custom document content in WPF. I set all those focusable parameters to true. Still, the focus is not on the textbox. Any thought or comment ?

So far, I've added these textbox.Focus(); textbox.SelectAll(); to the constructor of my WPF page.

textbox is in a Canvas inside a DockPanel, and all of them are part of a custom:DocumentContent.

thank you in advance,

+1  A: 

That should work. Check if textbox.Focus() is returning true, it will tell you if call did work. Also, try calling textbox.Focus() from Loaded event of Window/Page.

Andrija
+3  A: 

Take a look at this blog post and the MSDN Focus Overview article. From your question it sounds like you're trying to set focus in the constructor. The UI Elements have not been created at that point. You should set focus during the Loaded event of your control.

Bryan Anderson
You pointed out a very important issue that UI elements have not been created at the constructor. I also tried it with this, but no focus yet. Now I have another question, does it matter if I call textbox.Focus() in different places like constructor and loading events, and even on the XAML itself? thanksprivate void Document_Loaded(object sender, RoutedEventArgs e) { textbox.Focusable = true; textbox.Focus(); }
paradisonoir
you are right, I added to the "loaded" of the textbox itself, and it worked. I thought, if I add it to the custom:DocumentContent, it would help, but I had to add it to textbox itself. Thanks for response.
paradisonoir