Assigning Keyboard Shortcut / Access Keys to your WPF Application


Assigning shortcut/access keys to say a button (So instead of click on the Next Button you can press N on your keyboard) is something really trivial in a the Windows Form's world - but strangely in WPF the friendly '&' that you place in front of the Char of your Windows Forms Text property fails to work. At least at the time of writing this post all major search engines seems to return minimal information on how to set an access key for a button.

As it turns out in WPF you need to use the _ (underscore) char instead of a & to assign a shortcut key. Thanks to a colleague of mine who pointed out the reason for it being that using a '&' in XAML would require typing &amp due to XML syntax.

Rob Relyea and a few others from the VS 2008 Designer and Blend team discussed on an internal alias as to how to make it easier for developers moving from the Windows Forms/Visual Basic world to be made aware of this change. If you do have any suggestions do drop a comment below or you can also leave a comment on Rob's blog post on this topic here.


Comments (6)
  1. Assigning shortcut/access keys to say a button (So instead of click on the Next Button you can press

  2. Dhirendra Singh says:

    Thanks buddy,

    Really it has been a great help, i was searching from hours on Google, but got nowhere…

  3. Kenna says:

    How about assign F1 to a button?

  4. tim says:

    What if my content is an image and not assigned in the <Button> definition?

    <Button Name="btnSave" ToolTip="Save" Click="SaveData">

                           <Image VerticalAlignment="Center" Name="imgSave"  Source/images/save24.png" ></Image>

                       <?Button>

  5. Scruff.R says:

    That works fine for most of the combinations, but strangely enough "SomeOption_1" or so does work only with <Alt>+[1] on the main keyboard section, but not with <Alt>+[1] on the number pad. How’s that?

    Users are usually unaware of scancodes or so. So that might lead to some confusion.

  6. Vaishnavi says:

    I want F3 to be assigned to a button for next search. How do I do it?

Comments are closed.

Skip to main content