Set focus on TextBox in UserControl
Asked Answered
N

4

5

In my program I have a user control that displays data on a window using a content presenter. I would like to simply set the cursor focus on a certain textBox in my window at startup.

Usually I would do this through the code-behind of the window, like this: textBox.Focus();

However, the textBox is defined in the user control, and doesn't seem to work the same way. So far I have tried the same method as above in the user control's code-behind.

Why doesn't this work? How do I set the focus if the textBox is defined in a user control?

What I have tried....:

User Control:

public UserControl()
{
    InitializeComponent();

    FocusManager.SetFocusedElement(this, textBox);
}

User Control:

public UserControl()
{
    InitializeComponent();

    textBox.Focusable = true;
    Keyboard.Focus(textBox);
}
Nonoccurrence answered 17/10, 2013 at 18:8 Comment(1)
https://mcmap.net/q/125626/-wpf-and-initial-focusAdductor
R
5

Give this a try: FocusManager.SetFocusedElement

FocusManager.SetFocusedElement(parentElement, textBox)

or from the msdn website:

textBox.Focusable = true;
Keyboard.Focus(textBox);

Note: You can't set focus in a constructor. If you are, UI Elements have not been created at that point. You should set focus during the Loaded event of your control.

Rideout answered 17/10, 2013 at 18:11 Comment(4)
Okay, I was trying to set the focus in the constructor of the User Control. Where exactly is the loaded event of my control(Which would be the textBox)?Nonoccurrence
I have tried both of these things in the constructor of my user control. Neither had any effect.Nonoccurrence
private void Document_Loaded(object sender, RoutedEventArgs e) { textbox.Focusable = true; textbox.Focus();}Rideout
alright, do I have to attach that to any xaml so that the program will call to it? Edit: NVM, got it, thanks a lotNonoccurrence
B
3

A little bit late but what it really worked for my was

public UserControl()
    {
        InitializeComponent();

        Dispatcher.BeginInvoke(new System.Action(() => { Keyboard.Focus(TextBox); }),
                               System.Windows.Threading.DispatcherPriority.Loaded);
    }
Boltonia answered 27/12, 2016 at 7:53 Comment(1)
B
2

You can try setting the focus in the Loaded or Initialized event of the User control. Eg:

private void MyWpfControl_Load(object sender, EventArgs e)
{
    textBox.Focusable = true;
    Keyboard.Focus(textBox);
}

Info: Loaded event or Initialized event

Bimbo answered 17/10, 2013 at 18:41 Comment(0)
T
0

If you focus your control in constructor or in an event (like Loaded) every user control of same type got focus when initialized in same window. And the last one wins the focus. Do this instead;

  1. Define a Focus method with new keyword in user control:

public new bool Focus() => textBox.Focus();

  1. Call which you want to active from windows's code behind file

userControl.Focus();

Thagard answered 6/8, 2024 at 15:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.