本文介绍了KeyBinding风格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用类似于RelayCommand的ICommand类和类似的变体,但扩展属性往往与命令一起使用。除了设置这些属性作为命令设置的一部分,我可以在绑定时使用它们。例如

I use an ICommand class similar to RelayCommand and similar variations of that, but with extended properties that tend to go with a command. In addition to setting such properties as part of the Command setup, I can use them when binding. For example

<UserControl.InputBindings>
    <KeyBinding Command="{Binding SaveCommand}" Key="{Binding SaveCommand.GestureKey}" Modifiers="{Binding SaveCommand.GestureModifier}" />
</UserControl.InputBindings>

现在我想做的是有一个风格来利用这一点,像代码下面。它不起作用,因为显然KeyBinding没有DataContext。有没有什么可以做这个绑定或类似的工作?

Now what I would like to do is have a style to take advantage of this, something like the code below. It doesn't work though, since apparently KeyBinding doesn't have a DataContext. Is there someway I can make this binding or something similar to it work?

干杯,

Berryl

Cheers,
Berryl

<Style x:Key="KeyBindingStyle" TargetType="{x:Type KeyBinding}">
    <Setter Property="Command" Value="{Binding Command}" />
    <Setter Property="Gesture" Value="{Binding GestureKey}" />
    <Setter Property="Modifiers" Value="{Binding GestureModifier}" />
</Style>

<UserControl.InputBindings>
    <KeyBinding DataContext="{Binding SaveCommand}" />
</UserControl.InputBindings>



更新



以下是第一遍沿着HB扩展KeyBinding正在建议,以及我的扩展Command类的基本结构。绑定不会用此错误编译:在'KeyBindingEx'类型的'CommandReference'属性中不能设置'Binding'。 绑定只能在DependencyObject的DependencyProperty上设置。

UPDATE

Below is a first pass at extending KeyBinding along the lines that H.B. is suggesting, along with the basic structure of my extended Command class. The binding doesn't compile with this error: A 'Binding' cannot be set on the 'CommandReference' property of type 'KeyBindingEx'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

<UserControl.InputBindings>
    <cmdRef:KeyBindingEx  CommandReference="{Binding SaveCommand}"/>
</UserControl.InputBindings>

   public class KeyBindingEx : KeyBinding
{
    public static readonly DependencyProperty CommandReferenceProperty = DependencyProperty
        .Register("VmCommand", typeof(CommandReference), typeof(KeyBindingEx),          
                  new PropertyMetadata(new PropertyChangedCallback(OnCommandReferenceChanged)));

    private static void OnCommandReferenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var kb = (KeyBinding) d;
        var cmdRef = (VmCommand)e.NewValue;

        kb.Key = cmdRef.GestureKey;
        kb.Modifiers = cmdRef.GestureModifier;
        kb.Command = cmdRef;
    }

    public CommandReference CommandReference
    {
        get { return (CommandReference)GetValue(CommandReferenceProperty); }
        set { SetValue(CommandReferenceProperty, value); }
    }
} 

public class CommandReference : PropertyChangedBase, ICommandReference
{

    public Key GestureKey
    {
        get { return _gestureKey; }
        set
        {
            if (_gestureKey == value) return;

            _gestureKey = value;
            NotifyOfPropertyChange(() => GestureKey);
        }
    }
    private Key _gestureKey;

    ...
}


public class VmCommand : CommandReference, ICommand
{
    ...

    public KeyBinding ToKeyBinding()
    {
        return new KeyBinding
               {
                   Command = this, 
                   Key = GestureKey, 
                   Modifiers = GestureModifier
               };
   }

}


推荐答案

在WPF中,您可以创建一个,它全部为您或您的分配,您使用您的自定义命令类型的新属性对KeyBinding进行子类化,该属性应该连接一个属性更改的回调,然后可以再次设置其他属性。不能想像现在比现在更优雅。

In WPF you could either create a markup extension which does all the assignments for you or you subclass KeyBinding with a new property of your custom command type which should hook up a property changed callback in which then again can set the other properties. Cannot think of anything more elegant than that right now.

这篇关于KeyBinding风格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 03:19