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 & 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
Anonymous
December 15, 2007
Assigning shortcut/access keys to say a button (So instead of click on the Next Button you can pressAnonymous
November 18, 2008
Thanks buddy, Really it has been a great help, i was searching from hours on Google, but got nowhere...Anonymous
January 29, 2009
How about assign F1 to a button?Anonymous
February 13, 2009
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>Anonymous
February 16, 2009
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.Anonymous
February 11, 2010
I want F3 to be assigned to a button for next search. How do I do it?