开发者

KeyBinding style

开发者 https://www.devze.com 2023-03-13 18:34 出处:网络
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 Comma

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>

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?

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>

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()
    {
    开发者_StackOverflow中文版    return new KeyBinding
               {
                   Command = this, 
                   Key = GestureKey, 
                   Modifiers = GestureModifier
               };
   }

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消