1

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

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

4 Answers4

7

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="..." />
Mike Christiansen
  • 1,104
  • 2
  • 13
  • 30
igorushi
  • 1,855
  • 21
  • 19
1

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?

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
0

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.

JordanTDN
  • 181
  • 1
  • 12
0

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

Community
  • 1
  • 1
SRM
  • 1,377
  • 1
  • 10
  • 17