-
Notifications
You must be signed in to change notification settings - Fork 802
Description
Describe the bug
When a ContentDialog is shown which contains interactive controls, the display of the focus highlight is inconsistent:
- If the dialog is opened via a button click, the highlight is not shown. The first interactive control on the dialog is focussed, but the highlight is not shown.
- If the dialog is opened via a
KeyUpevent handler or in response to aKeyboardAcceleratorinvocation, the focus highlight is shown.
Steps to reproduce the bug
-
Create a new project in Visual Studio. Choose "Blank App, Packaged (WinUI 3 in Desktop)" as the project template.
-
Name the project "CheckboxTest".
-
Replace the
MainWindow.xamlfile contents with the XAML below:
<Window
x:Class="CheckboxTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CheckboxTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="ContentDialog Focus Test">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Name="ShowDialogButton" Content="Show Dialog" Click="ShowDialogButton_Click" />
<ContentDialog
x:Name="DeleteConfirmationDialog"
Title="Delete file"
PrimaryButtonText="Move to Recycle Bin"
CloseButtonText="Cancel"
DefaultButton="Close">
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Spacing="12">
<TextBlock TextWrapping="Wrap" Text="Are you sure you want to move file 'somefile.jpg' to the Recycle Bin?" />
<CheckBox x:Name="DeleteDontAskCheckbox" Content="Don't ask me again" />
</StackPanel>
</ContentDialog>
</StackPanel>
</Window>- Replace the
MainWindow.xaml.csfile code with:
using System;
using System.Threading.Tasks;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace CheckboxTest;
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
this.Content.KeyUp += Content_KeyUp;
}
private async void Content_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Delete)
{
if (await OpenDeleteDialog() == ContentDialogResult.Primary)
{
// ... do delete stuff
}
}
}
private async void ShowDialogButton_Click(object sender, RoutedEventArgs e)
{
if (await OpenDeleteDialog() == ContentDialogResult.Primary)
{
// ... do delete stuff
}
}
private async Task<ContentDialogResult> OpenDeleteDialog()
{
DeleteConfirmationDialog.XamlRoot = Content.XamlRoot;
return await DeleteConfirmationDialog.ShowAsync();
}
}- Run the project and click the "Show Dialog" button. Observe that the dialog is shown with no focus highlight present on the checkbox control:
-
Close the dialog by pressing Enter or selecting the "Cancel" button.
-
Press Delete. Observe that the dialog is shown with the first interactive element highlighted as the focussed control:
Expected behavior
The display of the focus highlight should be consistent, i.e. independent of the method used to show the ContentDialog. I suggest not showing the highlight until the user uses Tab to move the focus around the dialog's controls.
Screenshots
No response
NuGet package version
None
Windows version
Windows 11 (24H2): Build 26100
Additional context
Additional context may be found on my StackOverflow question here: https://stackoverflow.com/questions/79376328/winui-3-how-to-set-focus-to-a-contentdialog-button/79382988

