'Use of Keyboard Accelerator and Keyboard Access Keys in Windows 10 UWP with ContentDialog
When using a windows 10 UWP Content Dialog, the Page behind the dialog is 'grayed out' and the buttons and other controls do not respond to click event (Good!). However, I have discovered that the Keyboard Accelerators (CTRL-X for example), or Keyboard Access Keys (Alt-A for example) are still active. This can cause many problems (in my case, either re-calling the content dialog which give an error, or leaving the page while the dialog is still active). The only fix I have found is to manually disable all the affected controls when showing the content dialog, but I was hoping someone had a better solution.
UPDATE: Thanks for comments. I created a new project to double check, and I also double checked my own project. What I found is this: My mistake on KeyboardAccelerator (i.e. CTRL-X), that does not work (as it should not). However, I have confirmed that in my simple test project and my original, AccessKey (i.e. ALT-X) does indeed still work with the content dialog showing. See image below... Selecting ALT highlights the keys with the dialog open, and the pressing 's' or 'x' does indeed fire the button click event.
Solution 1:[1]
I think I found a pretty good solution (for my use case at least). I created a common function to call any ContentDialogs. I pass into that function the current Page. Before showing the dialog I set the Page.IsEnabled property to false, and then set back to true when exiting the function when the dialog is closed.
Solution 2:[2]
I wrote this blog post which solved it: https://reflectionit.nl/blog/2022/accelerator-keys-for-contentdialog-buttons. It uses a attached properties which can be used in the Style of the Buttons.
<ContentDialog x:Class="ContentDialogDemoUWP.ConfirmContentDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:helpers="using:ContentDialogDemoUWP.Helpers"
Title="Confirm"
CloseButtonText="Cancel"
PrimaryButtonText="Yes"
SecondaryButtonText="No">
<ContentDialog.PrimaryButtonStyle>
<Style BasedOn="{StaticResource AccentButtonStyle}" TargetType="Button">
<Setter Property="helpers:KeyboardAcceleratorsServices.KeyboardAccelerators">
<Setter.Value>
<helpers:KeyboardAcceleratorList>
<KeyboardAccelerator Key="Y" />
<KeyboardAccelerator Key="J" />
</helpers:KeyboardAcceleratorList>
</Setter.Value>
</Setter>
</Style>
</ContentDialog.PrimaryButtonStyle>
<ContentDialog.SecondaryButtonStyle>
<Style TargetType="Button">
<Setter Property="helpers:KeyboardAcceleratorsServices.KeyboardAccelerator">
<Setter.Value>
<KeyboardAccelerator Key="N" />
</Setter.Value>
</Setter>
</Style>
</ContentDialog.SecondaryButtonStyle>
<Grid>
<TextBlock Text="Are you sure you want this?" />
</Grid>
</ContentDialog>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Fons Sonnemans |

