title: WPF AccessText In Button Only For Alt
body: <p>I have a WPF Window (.NET 4.0) that opens another WPF window as a dialog using <code>ShowDialog()</code>.  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.</p>
  
  <p>I have an <code>AccessText</code> 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.  </p>
  
  <p>However, here are the problems:</p>
  
  <ul>
  <li><p>The access key works without alt.  As soon as the dialog opens I can press just the letter <kbd>S</kbd> or <kbd>C</kbd> without <kbd>alt</kbd> 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.</p></li>
  <li><p>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?</p></li>
  </ul>
  
  <p>Here is the code calling the dialog from the main window:</p>
  
  <pre><code>    private void Button_Click(object sender, RoutedEventArgs e)
      {
          var w = new DialogWindow();
          w.ShowDialog();
      }
  </code></pre>
  
  <p>Here is the XAML in the dialog window:</p>
  
  <pre><code>    &lt;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"&gt;
          &lt;Grid&gt;
              &lt;Grid.RowDefinitions&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
                  &lt;RowDefinition Height="Auto" /&gt;
              &lt;/Grid.RowDefinitions&gt;
  
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="Some Setting:" Margin="10"/&gt;
                  &lt;TextBox  Width="200" Margin="2" /&gt;
              &lt;/StackPanel&gt;
  
              &lt;StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Right"&gt;
                  &lt;Button Name="btnSave" Margin="10,10,5,10" Width="100" Height="25" Click="btnSave_Click" &gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Image Source="pack://siteoforigin:,,,/save_16xLG.png" /&gt;
                          &lt;AccessText Text="_Save" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
                  &lt;Button Name="btnCancel" Margin="5,10,10,10" Width="100" Height="25" Click="btnCancel_Click"&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;Image Source="pack://siteoforigin:,,,/action_Cancel_16xLG.png" /&gt;
                          &lt;AccessText Text="_Cancel" /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/Button&gt;
              &lt;/StackPanel&gt;
          &lt;/Grid&gt;
      &lt;/Window&gt;
  </code></pre>
  
  <p>Here is the code behind for the dialog window:</p>
  
  <pre><code>    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();
          }
      }
  </code></pre>