user3602045
user3602045

Reputation: 39

Instantly type in the text box, without having to click it WPF

I have tried Keyboard.Focus() in the initialize component. Doesnt work.

Im clicking on button to open a new window for an input from the user. I want the user to be able to instantly start typing without having to click on the text box first and then type.

Is there any simple way to get this donw

Upvotes: 0

Views: 1033

Answers (3)

ThomasAndersson
ThomasAndersson

Reputation: 1904

Managing Focus is hard! Without going in to the details, it is about getting things done in the right order, have I in the past been successfull with a brute force approach:

In the Loaded event of the window:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() =>
        {
          myTextBox.Focus();
          Keyboard.Focus(myTextBox);
          FocusManager.SetFocusedElement(myWindow, myTextBox);
        });

Upvotes: 0

Gopichandar
Gopichandar

Reputation: 2841

Make it simple.

<Window FocusManager.FocusedElement="{Binding ElementName=SomeElement}">

   <TextBox x:Name="SomeElement"/>
     ...

</Window>

Upvotes: 1

Jai
Jai

Reputation: 8363

Try doing it in Loaded event of the window. And, as far as I know, simply using MyTextBox.focus() should give it both logical focus and keyboard focus.

Upvotes: 2

Related Questions