Reputation: 39
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
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
Reputation: 2841
Make it simple.
<Window FocusManager.FocusedElement="{Binding ElementName=SomeElement}">
<TextBox x:Name="SomeElement"/>
...
</Window>
Upvotes: 1
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