【问题标题】:Bind Key of KeyBinding to Key Property将 KeyBinding 的 Key 绑定到 Key 属性
【发布时间】:2015-08-02 21:07:36
【问题描述】:

我正在尝试将 KeyBinding 的键属性绑定到这样的键属性:

<i:Interaction.Triggers>
    <local:InputBindingTrigger>
        <local:InputBindingTrigger.InputBinding>
            <KeyBinding Key="{Binding SettingsViewModel.PreviousHotkey}"/>
        </local:InputBindingTrigger.InputBinding>
        <cal:ActionMessage MethodName="FormKeyDown"/>
    </local:InputBindingTrigger>
</i:Interaction.Triggers>

PreviousHotkey 是 Key 类型的属性。

public Key PreviousHotkey
{
    get { return _previousHotkey; }
    set { _previousHotkey = value; }
}

据我了解,KeyBindings 是应用程序范围内的,所以哪个控件有焦点并不重要吗? 当我执行时,输出中出现以下错误:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=SettingsViewModel.PreviousHotkey; DataItem=null; target element is 'KeyBinding' (HashCode=24311996); target property is 'Key' (type 'Key')

它似乎也不起作用。

如何绑定 KeyBinding 的 Key 属性?

【问题讨论】:

    标签: c# wpf key-bindings


    【解决方案1】:

    更新

    我的解决方案有误,但由于我有义务至少努力提供一个可行的解决方案,因此我的建议如下:

    首先,使用Caliburn.MicroActionMessage属性时,自定义InputBindingTrigger似乎不允许Binding

    因此,既然InputBindingTrigger 已经在使用ICommand,为什么不省略Caliburn.Micro 而只使用简单的KeyBinding 命令呢?

    Xaml:

    <Window x:Class="MainWindow">
        <Window.InputBindings>
            <KeyBinding Key="{Binding MyKey}" Command="{Binding MyCommand}" 
                CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Key}"/>
        </Window.InputBindings>
        <Grid>
            ...
        </Grid>
    </Window>
    

    MainWindow.cs:

        public ICommand MyCommand { get; set; }
    
        Key _myKey ;
        public Key MyKey 
        {
            get
            {
                return _myKey;
            }
            set
            {
                _myKey = value;
                OnPropertyChanged("MyKey");
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            MyCommand = new KeyCommand(MyMethod);
            MyKey =  Key.F5;
        }
    
        public void MyMethod(object param)
        {
            var inputKey = param;
            // do something
        }
    

    KeyCommand 的实现:如果您还没有实现类似的东西。

    public class KeyCommand : ICommand
    {
        Action<object> _execute;
        public event EventHandler CanExecuteChanged = delegate { };
        public KeyCommand(Action<object> execute)
        {
            _execute = execute;
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }
    
        public void Execute(object param)
        {
            _execute(param);
        }
    }
    

    【讨论】:

    • 谢谢,我回家后检查一下! :)
    • 嗯,我刚试过这个。错误消失了,但 KeyBinding 似乎不起作用。它不会在使用密钥时触发。我在没有绑定的情况下尝试过,在 XAML 中设置了 Key 并且它也没有触发,所以它可能是一个不同的问题?
    • @TimKatheteStadler,抱歉,我的解决方案有误,请查看我的更新。
    • 还有一个问题,有什么方法可以将按下的键作为被调用方法的参数?
    • 您可以将CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Key}" 添加到您的KeyBinding。这样,您的自定义命令必须是参数化的(即 Action _execute )并且MyMethod 将采用参数(例如public void MyMethod(object param))。
    猜你喜欢
    • 2018-04-05
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 2012-10-01
    相关资源
    最近更新 更多