【问题标题】:KeyBinding style键绑定样式
【发布时间】:2023-03-31 09:25:01
【问题描述】:

我使用了一个类似于 RelayCommand 的 ICommand 类及其类似的变体,但具有倾向于与命令一起使用的扩展属性。除了将这些属性设置为命令设置的一部分之外,我还可以在绑定时使用它们。例如

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

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

干杯,
浆果

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

更新

下面是沿 H.B. 扩展 KeyBinding 的第一步。建议,以及我扩展的 Command 类的基本结构。绑定无法编译并出现此错误:无法在“KeyBindingEx”类型的“CommandReference”属性上设置“Binding”。 “绑定”只能在 DependencyObject 的 DependencyProperty 上设置。

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

}

【问题讨论】:

  • 您想要符合 Silverlight 限制的东西,对吧? (编辑:那些 Setter.Value 绑定似乎暗示其他...)

标签: wpf silverlight data-binding key-bindings


【解决方案1】:

在 WPF 中,您可以创建一个 markup extension 来为您完成所有分配,或者您使用自定义命令类型的新属性将 KeyBinding 子类化,该属性应连接属性更改回调,然后再次可以在其中设置其他属性.现在想不出比这更优雅的了。

【讨论】:

  • 您好!扩展 KeyBinding 对我来说感觉更舒服,因为我之前使用过这个类在代码中创建 KeyBindings,我的下一个 MarkUpExtension 将是我的第一个。但我的第一次尝试根本没有好处。你能看看我编辑过的帖子,让我指出一个更好的方向吗?
  • 你注册依赖属性的名字与你使用的名字不同,注册方法中的字符串"VmCommand"应该是"CommandReference"
猜你喜欢
  • 1970-01-01
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 2011-05-01
  • 2020-09-21
相关资源
最近更新 更多