WPF InputBinding Ctrl+MWheelUp/Down Possible?

你说的曾经没有我的故事 提交于 2021-02-08 06:39:40

问题


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


回答1:


It can be done using very simple custom MouseGesture:

public enum MouseWheelDirection { Up, Down}

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="..." />



回答2:


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?




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/4263237/wpf-inputbinding-ctrlmwheelup-down-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!