【问题标题】:KeyBinding not working with RelayCommandKeyBinding 不适用于 RelayCommand
【发布时间】:2016-03-15 15:31:23
【问题描述】:

我正在使用稍微改编的RelayCommand 将命令定向到我的视图模型,这工作正常,但由于某种原因,我根本无法让输入绑定工作。例如,我有一个这样的菜单:

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

当您单击菜单项时(显然)运行良好,但我在此处定义了快捷方式:

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

但是按 F5 没有任何作用(作为参考,绑定到 ApplicationCommands.Close 工作正常)。没有绑定错误(如果我将其更改为绑定不存在的FooCommand,我会立即收到绑定错误)而且我无法弄清楚这里缺少什么。

我的命令在我的视图模型中定义如下:

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;
    }
}

【问题讨论】:

  • 你必须在你的 ViewModel 构造函数中初始化你的命令,你可以有一个private set
  • @XAMlMAX:当您第一次尝试访问它时,它会延迟初始化(在 get 中)
  • 我也遇到过类似的问题,一旦我在构造函数中初始化它,一切都开始工作了。
  • @XAMlMAX:好的,我会试一试。编辑:似乎没有帮助。不过谢谢你的建议。
  • @J.H.:碰巧我还有一个没有 canExecute 的那个也不起作用 _openCommand = new RelayCommand(p =&gt; this.Open());

标签: wpf c#-4.0


【解决方案1】:

命令的绑定没有数据上下文(嗯,它使用窗口但未设置)。所以,你必须指定它。

这是我制作的完整、有效的测试用例:

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;
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-02
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多