繁体   English   中英

如何将值绑定到 MouseBinding class 的“手势”属性? – WPF XAML

[英]How to bind a value to the "Gesture" property of the MouseBinding class? – WPF XAML

我目前正在尝试将以下代码行添加到我的 XAML 文件中:

<MouseBinding Command="helix:ViewportCommands.Rotate" Gesture="{Binding ViewportRotateGesture}" />

问题是这行不通。 每当我尝试将字符串绑定到“手势”属性时,都会出现以下异常:

System.Windows.Markup.XamlParseException:不能在“MouseBinding”类型的“手势”属性上设置“绑定”。 只能在 DependencyObject 的 DependencyProperty 上设置“绑定”。

使用“MouseAction”属性而不是“Gesture”属性确实有效,但是这不允许您添加任何修饰符。 有人知道如何将鼠标手势与修饰符(例如 Shift+LeftClick)结合起来吗?

这不是一个 DependencyProperty,而是一个普通的 CLR 属性。 你不能束缚他。 请参阅公共虚拟 InputGesture 手势

为此,您可以创建行为或附加属性。

例子:

using System;
using System.Windows;
using System.Windows.Input;

namespace Core2022.SO.Chris.AttachedProperties
{
    public static class InputBinding
    {
        /// <summary>Returns the value of the attached property Gesture for the <paramref name="inputBinding"/>.</summary>
        /// <param name="inputBinding"><see cref="System.Windows.Input.InputBinding"/>  whose property value will be returned.</param>
        /// <returns>Property value <see cref="InputGesture"/>.</returns>
        public static InputGesture GetGesture(System.Windows.Input.InputBinding inputBinding)
        {
            return (InputGesture)inputBinding.GetValue(GestureProperty);
        }

        /// <summary>Sets the value of the Gesture attached property to <paramref name="inputBinding"/>.</summary>
        /// <param name="inputBinding"><see cref="System.Windows.Input.InputBinding"/> whose property is setting to a value..</param>
        /// <param name="value"><see cref="InputGesture"/> value for property.</param>
        public static void SetGesture(System.Windows.Input.InputBinding inputBinding, InputGesture value)
        {
            inputBinding.SetValue(GestureProperty, value);
        }

        /// <summary><see cref="DependencyProperty"/> for methods <see cref="GetGesture(System.Windows.Input.InputBinding)"/>
        /// and <see cref="SetGesture(System.Windows.Input.InputBinding, InputGesture)"/>.</summary>
        public static readonly DependencyProperty GestureProperty =
            DependencyProperty.RegisterAttached(
                nameof(GetGesture).Substring(3),
                typeof(InputGesture),
                typeof(InputBinding),
                new PropertyMetadata(null, OnGestureChanged));

        private static void OnGestureChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not System.Windows.Input.InputBinding inputBinding)
                throw new NotImplementedException($"Implemented only for the \"{typeof(System.Windows.Input.InputBinding).FullName}\" class");

            inputBinding.Gesture = (InputGesture)e.NewValue;
        }
    }
}
<MouseBinding Command="helix:ViewportCommands.Rotate"
              ap:InputBinding.Gesture="{Binding ViewportRotateGesture}"/>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM