简体   繁体   中英

How can I set the focus of a TextBox in WPF

I want to have focus in my TextBox after it is double-clicked.

I simply try:

myTextBox.Focus()

The caret appears however it is not blinking, and no one can type in the text box. What is the proper way to set the keyboard input focus on a TextBox ?

Old question, but it was the first to come in a search and I don't see a valid answer, so posting one.

Problem is probably that your TextBox has Logical Focus, but not Keyboard Focus. Logical focus is set per scope, and WPF allows you to have multiple Focus Scopes in your application, so multiple controls can be logically focused, however only one control in your application can have Keyboard Focus.

myTextBox.Focus();          // Will set Logical Focus for myTextBox's Focus Scope
Keyboard.Focus(myTextBox);  // Will set Keyboard Focus to myTextBox

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