WPF 输入绑定 Ctrl+MWheelUp/Down 可能吗?

发布于 2024-10-03 22:39:05 字数 714 浏览 4 评论 0原文

有没有办法可以将命令绑定到 Ctrl+MheelUp/Down?您知道在浏览器中,您可以执行相同的操作来增大/减小字体大小吗?我想在 WPF 中复制这种效果。可能的?我正在查看 InputBinding > MouseBindingsMouseAction 似乎不支持鼠标卷轴。

* 我好像发过类似的问题,但是找不到了

Is there a way I can bind a Command to Ctrl+MWheelUp/Down? U know in a browser, you can do the same to increase/decrease font size? I want to replicate that effect in WPF. Possible? I was looking at InputBinding > MouseBindings and MouseAction does not seem to support Mouse Scrolls.

* I seem to have posted a similar question, but can't find it anymore

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黯淡〆 2024-10-10 22:39:05

完成:

public enum MouseWheelDirection { Up, Down}

public class MouseWheelGesture : MouseGesture
{
    public MouseWheelDirection Direction { get; set; }

    public MouseWheelGesture(ModifierKeys keys, MouseWheelDirection direction)
        : base(MouseAction.WheelClick, keys)
    {
        Direction = direction;
    }

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
    {
        var args = inputEventArgs as MouseWheelEventArgs;
        if (args == null)
            return false;
        if (!base.Matches(targetElement, inputEventArgs))
            return false;
        if (Direction == MouseWheelDirection.Up && args.Delta > 0
            || Direction == MouseWheelDirection.Down && args.Delta < 0)
        {
            inputEventArgs.Handled = true;
            return true;
        }

        return false;
    }
        
}

public class MouseWheel : MarkupExtension
{
    public MouseWheelDirection Direction { get; set; }
    public ModifierKeys Keys { get; set; }

    public MouseWheel()
    {
        Keys = ModifierKeys.None;
        Direction = MouseWheelDirection.Down;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return new MouseWheelGesture(Keys, Direction);
    }
}

可以使用非常简单的自定义 MouseGesture:在 xaml 中

<MouseBinding Gesture="{local:MouseWheel Direction=Down, Keys=Control}" Command="..." />

It can be done using very simple custom MouseGesture:

public enum MouseWheelDirection { Up, Down}

public class MouseWheelGesture : MouseGesture
{
    public MouseWheelDirection Direction { get; set; }

    public MouseWheelGesture(ModifierKeys keys, MouseWheelDirection direction)
        : base(MouseAction.WheelClick, keys)
    {
        Direction = direction;
    }

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
    {
        var args = inputEventArgs as MouseWheelEventArgs;
        if (args == null)
            return false;
        if (!base.Matches(targetElement, inputEventArgs))
            return false;
        if (Direction == MouseWheelDirection.Up && args.Delta > 0
            || Direction == MouseWheelDirection.Down && args.Delta < 0)
        {
            inputEventArgs.Handled = true;
            return true;
        }

        return false;
    }
        
}

public class MouseWheel : MarkupExtension
{
    public MouseWheelDirection Direction { get; set; }
    public ModifierKeys Keys { get; set; }

    public MouseWheel()
    {
        Keys = ModifierKeys.None;
        Direction = MouseWheelDirection.Down;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return new MouseWheelGesture(Keys, Direction);
    }
}

in the xaml:

<MouseBinding Gesture="{local:MouseWheel Direction=Down, Keys=Control}" Command="..." />
顾铮苏瑾 2024-10-10 22:39:05

好吧,我在我的 ShellView : Window 中做了类似的事情,

this.KeyDown += (s, e) =>
{
    _leftCtrlPressed = (e.Key == Key.LeftCtrl) ? true : false;
};

this.MouseWheel += (s, e) =>
{
    if (_leftCtrlPressed) {
        if (e.Delta > 0)
            _vm.Options.FontSize += 1;
        else if (e.Delta < 0)
            _vm.Options.FontSize -= 1;
    }
};

我认为 Behaviour 方法会让事情变得更干净、更可重用,但我并没有真正明白。如果有人在这里用简单的方式解释一下那就太好了?

Ok, I did something like this in my ShellView : Window

this.KeyDown += (s, e) =>
{
    _leftCtrlPressed = (e.Key == Key.LeftCtrl) ? true : false;
};

this.MouseWheel += (s, e) =>
{
    if (_leftCtrlPressed) {
        if (e.Delta > 0)
            _vm.Options.FontSize += 1;
        else if (e.Delta < 0)
            _vm.Options.FontSize -= 1;
    }
};

I think the Behaviour method will make things cleaner and more reusable, but I didn't really get it. It'll will be great if someone explained it in a simple way here?

一梦等七年七年为一梦 2024-10-10 22:39:05

窗口有 MouseWheel 事件。您可以执行一些命令绑定魔术,然后将其绑定到 DataContext 属性。查看这篇 SO 文章以获取提示:在文本框 MVVM 内按下按键。另请查看这篇文章:http://code.msdn.microsoft.com/eventbehaviourfactor

Window has the MouseWheel event. You can do some command binding magic which you can then bind to a DataContext property. Check out this SO article for hints: Key press inside of textbox MVVM. Also take a look at this article: http://code.msdn.microsoft.com/eventbehaviourfactor

魂归处 2024-10-10 22:39:05

我只是使用 Interaction.Triggers 绑定命令。

您需要在 XAML 中引用表达式交互命名空间。

 <i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewMouseWheel">
        <cmd:InvokeCommandAction Command="{Binding MouseWheelCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

然后在相关命令中。

 private void MouseWheelCommandExecute(MouseWheelEventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
        {
            if (e.Delta > 0)
            {
                if (Properties.Settings.Default.ZoomLevel < 4)
                    Properties.Settings.Default.ZoomLevel += .1;
            }
            else if (e.Delta < 0)
            {
                if (Properties.Settings.Default.ZoomLevel > 1)
                    Properties.Settings.Default.ZoomLevel -= .1;
            }
        }

    }

如果 Delta 上升,则鼠标向上滚动;如果 Delta 下降,则鼠标向下滚动。我在一个应用程序中使用此功能,其中可滚动内容中会发生滚动,但当按下任一 Ctrl 键时,应用程序实际上会缩放。

I simply bind the command using Interaction.Triggers.

You'll need to reference the expression interactivity namespace in XAML.

 <i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewMouseWheel">
        <cmd:InvokeCommandAction Command="{Binding MouseWheelCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>

Then in the associated command.

 private void MouseWheelCommandExecute(MouseWheelEventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
        {
            if (e.Delta > 0)
            {
                if (Properties.Settings.Default.ZoomLevel < 4)
                    Properties.Settings.Default.ZoomLevel += .1;
            }
            else if (e.Delta < 0)
            {
                if (Properties.Settings.Default.ZoomLevel > 1)
                    Properties.Settings.Default.ZoomLevel -= .1;
            }
        }

    }

If Delta is rising the mouse is scrolling Up, falling it is scrolling Down. I use this in an application where Scrolling will occur in scroll-able content but when either of the Ctrl keys are down, the application actually zooms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文