microsoft-ui-xaml icon indicating copy to clipboard operation
microsoft-ui-xaml copied to clipboard

Main Window steal focus of child window when the last element with focus is removed

Open FrenchShad0w opened this issue 3 years ago • 7 comments

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) : image After the click on the button the child window opens over the main window and have the focus: image 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 : image

Steps to reproduce the bug

  1. Open new WinUI3 Blank App packaged
  2. Add in the main window xaml a button and a stack panel containing a button image
  3. 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 image
  4. 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.

FrenchShad0w avatar Jun 01 '23 11:06 FrenchShad0w

Same issue for me ! Anyone can help ?

Moustoine avatar Jun 02 '23 10:06 Moustoine

#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

zhuxb711 avatar Jun 09 '23 02:06 zhuxb711

@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 avatar Sep 26 '23 11:09 tinmac

@tinmac Yes you are right ! After placing AllowFocusOnInteraction="False" on the button that is removed the issue is fixed. Thanks !

FrenchShad0w avatar Sep 26 '23 13:09 FrenchShad0w

Set AllowFocusOnInteraction="False" on Page/Window is unacceptable because the controls like TextBox which rely on Focus would not work anymore

zhuxb711 avatar Sep 27 '23 03:09 zhuxb711

@zhuxb711 try AllowFocusOnInteraction="True" it works for me, ie leaves child over parent AND controls can have focus.

tinmac avatar Sep 27 '23 07:09 tinmac

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>

vinayakkuradia avatar Mar 26 '24 20:03 vinayakkuradia