microsoft / microsoft/microsoft-ui-xaml

Focus unexpectedly moving from ToggleSwitch to RootScrollViewer when using with NavigationView

Open
#9,331 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area-NavigationView bug team-Controls
Dominant language
C++
Stars
8.5k
Forks
945
Avg merge
2d 7h
Merged PRs (30d)
105

Description

Describe the bug

When we use a ToggleSwitch together with a NavigationView, the focus is moving from ToggleSwitch to RootScrollViewer with the first click of the ToggleSwitch. The issue is only in the first click; the remaining time, the focus will remain in ToggleSwitch. The issue is not found when using ToggleSwitch with other controls, for example, buttons. Also the issue is happening when clicking a ToggleSwitch only after clicking a NavigationViewItem.
The minimal code that can reproduce the issue is given below.
Any help would be appreciated.
Thank you.

Steps to reproduce the bug
  1. Create a WinUI 3 application.
  2. In MainWindow.xaml file, replace the existing code with following code .
 <Grid>
     <NavigationView x:Name="nvSample">
         <NavigationView.MenuItems>
             <NavigationViewItem Icon="Play" Content="Menu Item1" />
             <NavigationViewItem Icon="Save" Content="Menu Item2" />
             <NavigationViewItem Icon="Refresh" Content="Menu Item3" />
             <NavigationViewItem Icon="Download" Content="Menu Item4" />
         </NavigationView.MenuItems>
     </NavigationView>
     <!--<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>-->
     <ToggleSwitch x:Name="ToggleSwitchCtrl" AutomationProperties.Name="simple ToggleSwitch" GotFocus="ToggleSwitch_GotFocus"/>
</Grid>
  1. Build and run the application.
  2. Click any NavigationViewItem.
  3. Then click ToggleSwitch.
  4. Using Track Focused Element debugging tool, observe the current focused element in Live Visual Tree.
  5. Click the ToggleSwitch again, and repeat the step no. 6 to see the currently focused element.
Expected behavior

Focus should be in ToggleSwitch every time while clicking it.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.4.4: 1.4.231219000

Windows version

No response

Additional context

Windows version : Windows 10, 22H2 : Build 19045.
Instead of debugging tools, the following code can also be used in GotFocus event handler of ToggleSwitch to see the Focus movement.
ToggleSwitchCtrl().Focus(Microsoft::UI::Xaml::FocusState::Keyboard);

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the minimal reproduction from MainWindow.xaml, using the NavigationViewItem and ToggleSwitch sequence described in the issue. Observe focus with Track Focused Element or the ToggleSwitch_GotFocus handler, then trace the NavigationView and RootScrollViewer focus behavior. Done means focus remains on the ToggleSwitch after every click.

Written by the indexing model from the issue text.

Assessment

Domain
desktop, frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.