Reputation: 2910
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.
The TextBox
is in a Canvas
inside a DockPanel
, and all of them are part of a custom:DocumentContent
.
thank you in advance,
Upvotes: 1
Views: 6179
Reputation: 16129
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.
Upvotes: 6
Reputation: 14473
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.
Upvotes: 1