Keybinding a RelayCommand
Asked Answered
R

6

27

I'm using the RelayCommand in my app. It's great for putting the code in the viewmodel, but how do I bind keystrokes to my command?

RoutedUICommand has its InputGestures property, which makes the command automatically be invoked when I press the keystroke. (As an added bonus, it even makes the keystroke display in the MenuItem.) Unfortunately, there's no reusable interface for RoutedUICommand's extra properties, so I can't make a RelayUICommand that gets the same magic.

I've already tried using InputBindings:

<Window.InputBindings>
    <KeyBinding Key="PageUp" Command="{Binding SelectPreviousLayerCommand}"/>
</Window.InputBindings>

But that gets me a runtime exception, because KeyBinding.Command isn't a dependency property. (Actually, what it complains about is that KeyBinding isn't even a DependencyObject.) And since my RelayCommand is a property on my ViewModel (as opposed to the static field that RoutedUICommand is designed for), databinding is the only way I know of to reference it from XAML.

How have you guys solved this? What's the best way to bind a keystroke to a RelayCommand?

Raptorial answered 21/6, 2009 at 13:58 Comment(0)
D
14

The Command property of the KeyBinding class doesn't support data binding. This issue is going to be solved in .NET 4.0 and you should be able to see it in the coming .NET 4.0 Beta 2 version.

Diestock answered 5/10, 2009 at 11:37 Comment(2)
Binding the Command property of the KeyBinding class in .NET 4.0 is described in an article at tomlev2.wordpress.com/2009/10/26/…Brushwood
Above link has moved to thomaslevesque.com/2009/10/26/…Slothful
C
17

I don't think you can do this from XAML, for exactly the reasons you describe.

I ended up doing it in the code-behind. Although it's code, it's only a single line of code, and still rather declarative, so I can live with it. However, I'd really hope that this is solved in the next version of WPF.

Here's a sample line of code from one of my projects:

this.InputBindings.Add(new KeyBinding(
    ((MedicContext)this.DataContext).SynchronizeCommand,
    new KeyGesture(Key.F9)));

SynchronizeCommand in this case is an instance of RelayCommand, and (obviously) F9 triggers it.

Custumal answered 21/6, 2009 at 14:47 Comment(0)
D
14

The Command property of the KeyBinding class doesn't support data binding. This issue is going to be solved in .NET 4.0 and you should be able to see it in the coming .NET 4.0 Beta 2 version.

Diestock answered 5/10, 2009 at 11:37 Comment(2)
Binding the Command property of the KeyBinding class in .NET 4.0 is described in an article at tomlev2.wordpress.com/2009/10/26/…Brushwood
Above link has moved to thomaslevesque.com/2009/10/26/…Slothful
S
10

You could subclass KeyBinding, add a CommandBinding dependency property that sets the Command property, and then add it to XAML like any other input binding.

public class RelayKeyBinding : KeyBinding
{
    public static readonly DependencyProperty CommandBindingProperty =
        DependencyProperty.Register("CommandBinding", typeof(ICommand), 
        typeof(RelayKeyBinding),
        new FrameworkPropertyMetadata(OnCommandBindingChanged));
    public ICommand CommandBinding
    {
        get { return (ICommand)GetValue(CommandBindingProperty); }
        set { SetValue(CommandBindingProperty, value); }
    }

    private static void OnCommandBindingChanged(
        DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var keyBinding = (RelayKeyBinding)d;
        keyBinding.Command = (ICommand)e.NewValue;
    }
}

XAML:

<Window.InputBindings>
    <RelayKeyBinding 
        Key="PageUp" 
        CommandBinding="{Binding SelectPreviousLayerCommand}" />
</Window.InputBindings>
Sanguinolent answered 28/9, 2009 at 23:45 Comment(1)
keyBinding.Command = e.NewValue; needs a cast so it should be like keyBinding.Command = (ICommand)e.NewValue; Right ??Dentelle
T
2

You can use the CommandReference class.

Very elegant code and works like a charm.

Take a look at: How do I associate a keypress with a DelegateCommand in Composite WPF?

It works the same with RelayCommands, but it won't update your CanExecutes since the CommandReference doesn't use call CommandManager.RequerySuggested All you need to do to achieve automatic CanExecute reevaluation is do the following change inside the CommandReference class

public event EventHandler CanExecuteChanged
{
     add { CommandManager.RequerySuggested += value; }
     remove { CommandManager.RequerySuggested -= value; }
}
Tolbert answered 11/4, 2011 at 8:5 Comment(0)
M
0

I create a Key binding in my view model that links the command and the Key like so

        this.KeyBinding = new KeyBinding();
        //set the properties
        this.KeyBinding.Command = this.Command;
        this.KeyBinding.Key = this.Key;
        //modifier keys may or may not be set
        this.KeyBinding.Modifiers = this.ModifierKeys;

then i create a collection of InputBinding items in my View Model root and add them to the windows InputBindings in my window's code behind

     foreach (var item in applicationViewModel.InputBindingCollection) {
        this.InputBindings.Add(item);
     }

its bad form to do stuff in the code behind i know, but i dont know how to do the binding yet, however im still working on it. :) The only thing this doent give me is a key command modifer list in the menu, but that is to come.

Marylynnmarylynne answered 25/9, 2009 at 0:54 Comment(0)
P
-1

Assuming your RoutedCommands are defined statically:

#region DeleteSelection

    /// <summary>
    /// The DeleteSelection command ....
    /// </summary>
    public static RoutedUICommand DeleteSelection
        = new RoutedUICommand("Delete selection", "DeleteSelection", typeof(ChemCommands));

    #endregion

Bind in XAML thus:

<Canvas.InputBindings>
    <KeyBinding Key="Delete" Command="{x:Static Controls:ChemCommands.DeleteSelection}" />
</Canvas.InputBindings>

Regards,

Tim Haughton

Putout answered 13/7, 2009 at 14:2 Comment(1)
The qustion about RelayCommands in a ViewModel and not about RoutedCommands.Canaan

© 2022 - 2024 — McMap. All rights reserved.