Welcome to MSDN Blogs Sign in | Join | Help

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.

Published Saturday, December 15, 2007 9:10 PM by mjeelani

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Assigning Keyboard Shortcut / Access Keys to your WPF Application

Saturday, December 15, 2007 11:29 AM by Noticias externas

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

# re: Assigning Keyboard Shortcut / Access Keys to your WPF Application

Wednesday, November 19, 2008 7:40 AM by Dhirendra Singh

Thanks buddy,

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

# re: Assigning Keyboard Shortcut / Access Keys to your WPF Application

Friday, January 30, 2009 2:01 AM by Kenna

How about assign F1 to a button?

# re: Assigning Keyboard Shortcut / Access Keys to your WPF Application

Friday, February 13, 2009 1:56 PM by tim

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>

# re: Assigning Keyboard Shortcut / Access Keys to your WPF Application

Monday, February 16, 2009 8:33 AM by Scruff.R

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.

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker