Main Window steal focus of child window when the last element with focus is removed
Describe the bug
In WinUI3 an app with a main window that can open a child window by click on a button and an other focusable element (here another unsued button) :
After the click on the button the child window opens over the main window and have the focus:
At this point if in the main window the last element with the focus is removed (here the button that opens the child window because it is the last element clicked) then the main window steals the focus and is bring in front of the child window :
Steps to reproduce the bug
- Open new WinUI3 Blank App packaged
- Add in the main window xaml a button and a stack panel containing a button
- Add in the main window code behind the event after the click in the button inside the stack panel. This event opens a second window and after a delay of 5 seconds remove the button inside the stack panel
- Run the project click on the button "Open second window", wait 5 seconds and the main window will steal the focus.
Expected behavior
When the child window has the focus the main window should not be able to steal it. After testing this on a WPF app and an UWP app, this behaviour is only present in WinUI 3.
Screenshots
No response
NuGet package version
WinUI 3 - Windows App SDK 1.3.1: 1.3.230502000
Windows version
Windows 11 (21H2): Build 22000
Additional context
After investigation this bug happens because in WinUI3 the focus changes when the last element with the focus is removed. It is like if you switch focus with tab on your keyboard. In UWP and WPF it seems that when the last element with the focus is removed the the focus is not reassigned.
In the example if you removed the unused button then the bug disappears because there is no other element to give the focus.
Same issue for me ! Anyone can help ?
#8087 Same here but WinUI team do not want to response. However with the latest Win11 & Windows App Sdk 1.3.1, I do not meet this issue anymore
@FrenchShad0w I had the same issue, a fix for me was placing AllowFocusOnInteraction="False" in the Winui 3 Parent Window/Page head xaml. This stops it focusing over child Windows when the child is opened from the parent. Before I added the line, the parent would cover the child.
@tinmac Yes you are right ! After placing AllowFocusOnInteraction="False" on the button that is removed the issue is fixed.
Thanks !
Set AllowFocusOnInteraction="False" on Page/Window is unacceptable because the controls like TextBox which rely on Focus would not work anymore
@zhuxb711 try AllowFocusOnInteraction="True" it works for me, ie leaves child over parent AND controls
can have focus.
I wanted to focus a new child window opened on double tapping a row of a DataGrid.
Setting AllowFocusOnInteraction="False" worked for me.
For those who are trying the same, can use below mentioned code:
<controls:DataGrid.RowStyle>
<Style TargetType="controls:DataGridRow">
<Setter Property="controls:DataGridRow.AllowFocusOnInteraction">
<Setter.Value>
<x:Boolean>False</x:Boolean>
</Setter.Value>
</Setter>
</Style>
</controls:DataGrid.RowStyle>