wpf - 鼠标绑定(bind)鼠标滚轮以放大 WPF 和 MVVM

标签 wpf data-binding mvvm mouse cinch

好的,我已经知道如何使用 LayoutTransform 和 ScaleTransform 来缩放 UI 元素网格。我不明白的是如何让我的 View 响应 CTRL+MouseWheelUp\Down 来做到这一点,以及如何使代码适合 MVVM 模式。

我的第一个想法是将 ZoomFactor 存储为一个属性,并绑定(bind)到一个命令来调整它。

我在看类似的东西:

<UserControl.InputBindings>
 <MouseBinding Command="{Binding ZoomGrid}" Gesture="Control+WheelClick"/>
</UserControl.InputBindings>

但我看到两个问题:

1)我认为没有办法判断轮子是向上还是向下移动,我也看不出如何确定多少。我见过 MouseWheelEventArgs.Delta,但不知道如何获得它。

2)绑定(bind)到 View 模型上的命令似乎不正确,因为它严格来说是一个 View 。

由于缩放严格来说只是 UI View ,我认为实际代码应该放在代码隐藏中。

你们将如何实现这一点?

p.s.,我使用 .net\wpf 4.0 使用 Cinch 进行 MVVM。

最佳答案

真正的答案是编写自己的 MouseGesture,这很容易。

<MouseBinding Gesture="{x:Static me:MouseWheelGesture.CtrlDown}"  
              Command="me:MainVM.SendBackwardCommand" />
public class MouseWheelGesture : MouseGesture
{
    public static MouseWheelGesture CtrlDown
        => new MouseWheelGesture(ModifierKeys.Control) { Direction = WheelDirection.Down};

    public MouseWheelGesture(): base(MouseAction.WheelClick)
    {
    }

    public MouseWheelGesture(ModifierKeys modifiers) : base(MouseAction.WheelClick, modifiers)
    {
    }

    public WheelDirection Direction { get; set; }

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
    {
        if (!base.Matches(targetElement, inputEventArgs)) return false;
        if (!(inputEventArgs is MouseWheelEventArgs args)) return false;
        switch (Direction)
        {
            case WheelDirection.None:
                return args.Delta == 0;
            case WheelDirection.Up:
               return args.Delta > 0;
            case WheelDirection.Down:
                return args.Delta < 0;
            default:
                return false;
        }
    }

    public enum WheelDirection
    {
      None,
      Up,
      Down,
    }
}

关于wpf - 鼠标绑定(bind)鼠标滚轮以放大 WPF 和 MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2271342/

相关文章:

c# - 有没有办法在 C# 4.5 中存储临时列表值?

wpf - 可以在开源项目中使用适用于 WPF 的 Telerik rad 控件吗?

wpf - 在 XAML 中绑定(bind)到窗口事件

.net - WPF/银光VS WinRT

c# - 我的 CollectionViewSource 没有获取更改

javascript - Angular 7 数据绑定(bind)被延迟

c++ - 标签上的绑定(bind)循环警告

objective-c - 在不相关的(表) View Controller 之间交换数据?

WPF 绑定(bind)到数据上下文

wpf - 有助于理解 MVVM 模式?