locked
uwp textbox auto focus when navigated back RRS feed

  • Question

  • A textblock and a textbox in a uwp page ,first textbox not focused,then press home button navigate from this app,and then back to this app, the textbox get focused .My question is how to disable this behavior(textbox not focused when navigated back)
    Friday, July 22, 2016 3:49 AM

Answers

  • Hi cj_tianxingzhe,

    That occurs because of your control is first in VisualTree.

    you can set focus to another control using C#

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

    or in XAML

    <Control1 TabIndex="0"/>

    above Control1 have to inherit  form Windows.UI.XAML.Controls.Control class  , for example Button, TextBox... 

    ( Note Grid, StackPanel...  doesn't inherit fro Control class , they inherit from Panel class therefore doesn't support TabIndex)

    if you have for example a button in  your XAML set its TabIndex=0 then when you navigate to that page the button get focus. 


    Friday, July 22, 2016 8:02 AM

All replies

  • Hi cj_tianxingzhe,

    That occurs because of your control is first in VisualTree.

    you can set focus to another control using C#

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

    or in XAML

    <Control1 TabIndex="0"/>

    above Control1 have to inherit  form Windows.UI.XAML.Controls.Control class  , for example Button, TextBox... 

    ( Note Grid, StackPanel...  doesn't inherit fro Control class , they inherit from Panel class therefore doesn't support TabIndex)

    if you have for example a button in  your XAML set its TabIndex=0 then when you navigate to that page the button get focus. 


    Friday, July 22, 2016 8:02 AM
  • Hi Azat Tazayan,

        Thanks for your answer, I have fixed it follow your method that using a button in my page ,set it's width and height to 1 pixel, TabIndex=0.

    By the way ,OnNavigatedTo will not fired when navigated back after press home button.....

    Friday, July 22, 2016 8:17 AM
  • Hi Azat Tazayan,

        Thanks for your answer, I have fixed it follow your method that using a button in my page ,set it's width and height to 1 pixel, TabIndex=0.

    By the way ,OnNavigatedTo will not fired when navigated back after press home button.....

    if you navigate back from the page then invoking the OnNavigatedFrom method 
    Friday, July 22, 2016 8:45 AM
  • Hi , My meaning is :  The OnNavigatingFrom and OnNavigatedFrom will not fired ,when press home button, and the

    OnNavigatedTo will not fired when press back button return back to app.

    Friday, July 22, 2016 8:56 AM
  • What do you mean when saying Home Button ? You can pat that C# code where you want.
    Friday, July 22, 2016 9:07 AM
  • Phone's physic home button
    Friday, July 22, 2016 9:12 AM
  • Phone's physic home button
    In your case you switch to start screen and your app go to suspending. if interesting  read this 
    Friday, July 22, 2016 3:56 PM
  • Ok ,thanks
    Monday, July 25, 2016 2:22 AM