2

I have a WPF Window (.NET 4.0) that opens another WPF window as a dialog using ShowDialog(). On that dialog window I have a save and cancel button that I want to wire up alt+S and alt+C for save and cancel shortcuts respectively.

I have an AccessText element in the buttons that display the underscore on the button for the letter following the underscore in the designer. This works when the user presses the alt key. Also the shortcut is working when the user presses the access key.

However, here are the problems:

  • The access key works without alt. As soon as the dialog opens I can press just the letter S or C without alt and button click event fires. If focus is on an input control (i.e. textbox) then it works only with alt, which is what I want. However, I only want the access key to work with alt when the form has focus.

  • After you press alt the first time, the underscores appear in the button text forever unless you start to type in an input control (i.e. textbox). I would have expected them to hide if I pressed alt again. Is this normal for WPF?

Here is the code calling the dialog from the main window:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var w = new DialogWindow();
        w.ShowDialog();
    }

Here is the XAML in the dialog window:

    <Window x:Class="WpfApplication2.DialogWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="122" Width="330" Topmost="True">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Some Setting:" Margin="10"/>
                <TextBox  Width="200" Margin="2" />
            </StackPanel>

            <StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right">
                <Button Name="btnSave" Margin="10,10,5,10" Width="100" Height="25" Click="btnSave_Click" >
                    <StackPanel Orientation="Horizontal">
                        <Image Source="pack://siteoforigin:,,,/save_16xLG.png" />
                        <AccessText Text="_Save" />
                    </StackPanel>
                </Button>
                <Button Name="btnCancel" Margin="5,10,10,10" Width="100" Height="25" Click="btnCancel_Click">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="pack://siteoforigin:,,,/action_Cancel_16xLG.png" />
                        <AccessText Text="_Cancel" />
                    </StackPanel>
                </Button>
            </StackPanel>
        </Grid>
    </Window>

Here is the code behind for the dialog window:

    public partial class DialogWindow : Window
    {
        public DialogWindow()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }
    }
Brian
  • 14,610
  • 7
  • 35
  • 43
Avery
  • 249
  • 1
  • 4
  • 11

1 Answers1

1

It doesn't hide when pressing ALT. It hides when focus is changed to another control.

When you don't have focus in either of the TextBoxes, you have focus on the Button. By design, WPF responds to hot keys when focus is on the Button, even if you don't press Alt.

Please see this answer for a workaround:

Access key getting selected even we did not press 'Alt' key in WPF

Community
  • 1
  • 1
Troels Larsen
  • 4,462
  • 2
  • 34
  • 54
  • 1
    I tried adding a second text box, and the underscore does hide when switching focus from one control to another via a mouse click, but switching focus via tabbing has no effect. Also the form still closes immediately if I press 'S' or 'C" regardless of whether I have pressed ALT or not if one of the textboxes does not have focus. – Avery Jul 11 '14 at 21:59
  • 1
    The link to the workaround provided the answer I needed for preventing access keys from working unless ALT is pressed. Thanks! – Avery Jul 11 '14 at 22:37