none
[UWP][XAML]Remove Focus From All Controls At Startup RRS feed

  • Question

  • I have a UWP app in which I want none of the controls to have focus at startup. All of my controls have the AllowFocusOnInteraction="False" attribute. However, the first control is still focused at startup. What can I do to prevent the app from giving focus to a control at startup? Thanks.

    Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/

    Tuesday, October 8, 2019 4:56 PM

All replies

  • Hi Nathan Sokalski,

    In advance, sorry I didn't write real code, so this is only my guess.
    How about this?
    Create a dummy control which is not visible, and make it focused at startup.

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Tuesday, October 8, 2019 11:57 PM
  • Hi,

    As @Ashidacchi said, you can create an extra control, but set its size to zero instead of collapsed. And set the focus to the control. It's better to set focus to another control to remove the focus. For example, the first control is a TextBox and I set a Button to remove its focus.

    .xaml:

    <StackPanel>           

    <Button x:Name="MyButton" Width="0" Height="0"></Button>           

    <TextBox Text="mytext" Margin="30"></TextBox>

    </StackPanel>

    .cs:

    private void MainPage_Loaded(object sender, RoutedEventArgs e)       

    {

    MyButton.Focus(FocusState.Programmatic);       

    }

    Best Regards,

    Fay


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 9, 2019 3:27 AM
  • I will add to above answers following: you will always have at least one logically focused control, however focus visual of that control can be either hidden or visible. If you want hide focus visual (a border appearing when you press Tab), call

    Focus(FocusState.Pointer)

    MCSD App Builder, MCSA UWP

    Friday, October 11, 2019 10:03 AM