Assign Short Cut Key to a button in WPF

user372724 picture user372724 · Jul 14, 2010 · Viewed 54.1k times · Source

How to assign short-cut key to a button in WPF?

Googling gave me the answer as to append _ instead of '&' in standard Winforms.

So after I have done as below :

<Button Name="btnHelp" Content="_Help"></Button> 

I did not find 'H' underlined.

That is first issue.

Second issue is that, how to execute that after pressing Alt + H at run-time. Say just to display a message box is sufficient for the example sake.

I am using C#, WPF

Thanks.

Answer

jhenninger picture jhenninger · Aug 14, 2014

This is kind of old, but I ran into the same issue today.
I found that the simplest solution is to just use an AccessText element for the button's content.

<Button Command="{Binding SomeCommand}">
    <AccessText>_Help</AccessText>
</Button>

When you press the Alt key, the 'H' will be underlined on the button.
When you press the key combination Alt+H, the command that is bound to the button will be executed.

http://social.msdn.microsoft.com/Forums/vstudio/en-US/49c0e8e9-07ac-4371-b21c-3b30abf85e0b/button-hotkeys?forum=wpf