How to enable KeyBinding for DelegateCommand in Prism 4?

3.3k Views Asked by At

I would like to enable a KeyBinding for a DelegateCommand in a Prism4 desktop application. For example, in my XAML file I have this:

<Grid.InputBindings>
        <KeyBinding Gesture="CTRL+A" Command="{Binding Command3}"/>
</Grid.InputBindings>

<StackPanel>
   <Button Grid.Row="0" Grid.Column="1" Content="HitMe" prism:Click.Command="{Binding  Command3}" />
</StackPanel>

and in my ViewModel I have this:

public DelegateCommand<string> Command3 { get; private set; }

    private void ExecuteCommand3(string commandParameter)
    {
        Debug.WriteLine("ExecuteCommand3");

    }

    private bool CanExecuteCommand3(string commandParameter)
    {
        return true;
    }

When I press the HitMe button the debug line outputs but pressing CTRL+A has no effect.

I have considered using the CommandReference class from TestMvvmExample2341 but that seems to duplicate the functionality of Prism 4 mechanisms.

Is there a an easy way to have CTRL+A invoke the Command3 in Prism4?

1

There are 1 best solutions below

2
On

That's it, maybe your problem is related to the Focus in your view, try this:

At runtime set the focus over the Button and then apply the keystroke. Also take a look at these posts:

WPF MVVM KeyBinding not being recognized right away and not always working

http://joyfulwpf.blogspot.com/2009/05/mvvm-commandreference-and-keybinding.html