microsoft / microsoft/microsoft-ui-xaml
Focus unexpectedly moving from ToggleSwitch to RootScrollViewer when using with NavigationView
Nobody has claimed this yet.
- 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
- Create a WinUI 3 application.
- 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>
- Build and run the application.
- Click any NavigationViewItem.
- Then click ToggleSwitch.
- Using Track Focused Element debugging tool, observe the current focused element in Live Visual Tree.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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