Matt Burland
Matt Burland

Reputation: 45135

KeyBinding not working with RelayCommand

I'm using a slightly adapted RelayCommand to direct commands to my view model and this works fine, but for some reason, I can't get input bindings to work at all. For example, I have a menu like this:

<Grid DataContext="{StaticResource app}">
...
    <MenuItem Header="_Run" IsEnabled="{Binding HasScript}">
        <MenuItem Header="_Run" Command="{ Binding RunCommand }" Style="{StaticResource menuEnabledStyle}" InputGestureText="F5"/>
        ...
    </MenuItem>
</Grid>

This runs fine when you click the menu item (obviously), but I have the shortcut defined here:

<Window.InputBindings>
    <KeyBinding Key="F5"
                Command="{Binding RunCommand}"/>
    <KeyBinding Modifiers="Alt" Key="F4"
                Command="ApplicationCommands.Close"/>
</Window.InputBindings>

But hitting F5 does nothing (for reference, the binding to ApplicationCommands.Close works just fine). There are no binding errors (if I change it to bind the non-existent FooCommand I immediately get a binding error) and I can't figure out what it's missing here.

My command is defined in my view model like this:

private RelayCommand _runCommand;
public ICommand RunCommand
{
    get
    {
        if (_runCommand == null)
        {
            _runCommand = new RelayCommand(p => { this.CurrentScript.Run(); }, p => this.CurrentScript != null && !this.CurrentScript.IsRunning);
        }
        return _runCommand;
    }
}

Upvotes: 1

Views: 716

Answers (1)

J.H.
J.H.

Reputation: 4322

The Command's Binding doesn't have a data context (well, its using the windows but it isn't set). So, you have to specify it.

Here is the complete, working test case I made:

XAML:

<Window x:Class="WpfApplication30.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication30"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:VM x:Key="app" />
    </Window.Resources>
    <Window.InputBindings>
        <KeyBinding Key="F5" Command="{Binding RunCommand, Source={StaticResource app}}" />
    </Window.InputBindings>
    <Grid DataContext="{StaticResource app}">
        <Menu>
            <MenuItem Header="_File">
                <MenuItem Header="_Run" Command="{Binding RunCommand}" InputGestureText="F5" />
            </MenuItem>
        </Menu>
    </Grid>
</Window>

CS:

using System.Windows;
using Microsoft.TeamFoundation.MVVM;

namespace WpfApplication30
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class VM
    {
        private RelayCommand _runCommand;
        public RelayCommand RunCommand
        {
            get
            {
                if (_runCommand == null)
                {
                    _runCommand = new RelayCommand(p => { MessageBox.Show("RunCommand"); });
                }
                return _runCommand;
            }
        }
    }
}

Upvotes: 2

Related Questions