So when my window opens, in the Loaded event, I set
MyTextBox.Focus();
and this works fine, Focus Visual is there, the caret blinks, I can type, no problem. Click on a node in a TreeView which I want to modify the textbox's text and re-focus the TextBox so the user can keep typing. I call:
MyTextBox.Focus();
one more time. The focus visual does NOT appear on the textbox, and the cursor is NOT in the textbox. However, Keyboard.FocusedElement == MyTextBox is TRUE. Two oddities:
A) I have a button [_Save] on the window. If I just press 'S' (no Alt- modifier), that button is pressed, like the window was taking my keystrokes!
B) if I press TAB on my keyboard, the first control on the window is selected, eventually, I'm able to select the control I want and it works perfectly.
There are no explicit focus scopes set anywhere.
Additionally, if I try traversals or InputManager.Current.ProcessInput(some tabs) or a few other ways to try to course the window into giving MyTextBox the focus properly, none of them work.
This is a regular WPF app without any special styling/templating, and I'm at my wit's end!
Keyboard.Focus(MyTextBox)
? – mm8TreeView
tries to capture focus after click - try postponingtextBox.Focus();
usingDispatcher
:textBox.Dispatcher.BeginInvoke((Action)(() => textBox.Focus()));
– Quercus