tags:

views:

81

answers:

1

I know about "_" instead of "&" but how to assign F1,F2... key to a button ? It's a small application, i'm not using Commands just straight click event handlers but I could use commands if necessary

+1  A: 

In the small project I'm working on now, I have this:

<Window.Resources>
    <c:CommandReference x:Key="ExitCommandReference" Command="{Binding ExitCommand}" />
    <c:CommandReference x:Key="ReloadCommandReference" Command="{Binding ReloadCommand}" />
</Window.Resources>
<Window.InputBindings>
    <KeyBinding Key="F4" Modifiers="Alt" Command="{StaticResource ExitCommandReference}" />
    <KeyBinding Key="F5" Command="{StaticResource ReloadCommandReference}"/>
</Window.InputBindings>

And in the code:

private DelegateCommand exitCommand;
private DelegateCommand reloadCommand;
public ICommand ExitCommand { get { return exitCommand ?? (exitCommand = new DelegateCommand(Exit)); } }
public ICommand ReloadCommand { get { return reloadCommand ?? (reloadCommand = new DelegateCommand(Reload)); } }
private static void Exit() { Application.Current.Shutdown(); }
private void Reload() { LoadData(); }
snurre
Too much code IMHO; I ended up by creating a custom button class with a "Key" attached property and on PreviewKeyUp I'm searching for the first child button with the right Key property
Catalin DICU