0

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?

Tom
  • 1
  • 1
  • 1

1 Answers1

2

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

Community
  • 1
  • 1
mascab
  • 175
  • 2
  • 6
  • Thanks mascab! That was it. I added a call to firstButtonName.Focus() within the UserControl_Loaded event so that no tabbing is required. – Tom Feb 26 '11 at 19:05
  • 1
    Hello Tom, that's great, but if you are planning to use MVVM with Prism maybe setting the focus in the code-behind will break the MVVM pattern. I recommend that you is to use the _FocusManager_, take a look to this post [link](http://stackoverflow.com/questions/1178449/wpf-mvvm-focus-field-on-load) – mascab Feb 28 '11 at 18:28