简体   繁体   中英

Problem with TextBox Focus in WPF

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,

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM