WPF always focuses on the text field

I always want Focus on a specific TextBox in my WPF application, whenever I click on something in the application, it should always focus on the TextBox . Is it possible, and how to consult and help. Thanks!!!

+4
source share
3 answers

There is a MouseLeftMouseButton event MouseLeftMouseButton . When the event handler was fired, use textbox.Focus() inside the handler.

+1
source

Add a handler to the TextBox.OnLostFocus event that sets focus on the TextBox.

+3
source

If I'm right, you intend to get keyboard commands and display char in the text box, even if the focus is on other controls.

If so, you can redirect keyboard commands to the root control (a control at the top level ... for example: a window), analyze them and display them in a text box. I tried to give examples if that helps.

EDIT:

 private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) { if (Keyboard.Modifiers != ModifierKeys.Shift) { if (e.Key > Key.A && e.Key < Key.Z) { textBox1.Text += e.Key.ToString().ToLower(); } } else { if (e.Key > Key.A && e.Key < Key.Z) { textBox1.Text += e.Key.ToString(); } } e.Handled = true; } 
+1
source

Source: https://habr.com/ru/post/1300962/


All Articles