Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put the focus to a TextBox in OnNavigatedTo event?

In Windows 8, when a page is navigated, I need to put the focus to a specific TextBox.

I used:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}

But it does not work. However, if I call myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard); from a button click event, it works fine.

How can I set the focus to a TextBox when the page is loaded?

like image 983
Dabiel Kabuto Avatar asked Dec 01 '22 20:12

Dabiel Kabuto


2 Answers

Try calling myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard); with this.Dispatcher

A code sample:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);
    Dispatcher.RunAsync(
        CoreDispatcherPriority.Normal,
        () => myControl.Focus(FocusState.Keyboard));
}
like image 176
MichaelS Avatar answered Dec 04 '22 08:12

MichaelS


Try this:

myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);

and set this control(mycontrol) as the first control in your XAML code.

like image 26
user3051456 Avatar answered Dec 04 '22 09:12

user3051456