MouseBinding wheel rotation - both directions?
- I see I can use MouseAction.WheelClick to react to wheel rotation. Is there a way to create different MouseBinding for rotation in one and another direction, or the only way to react differently to rotation in different directions is to to forget MouseBinding and create appropriate mouse events handlers?
Thanks,
Martin
Answers
Try using the Mouse.MouseWheel event. The handler receives a MouseWheelEventArgs parameter that has a Delta property that is positive when the user scrolls the mousewheel up and negative when he (or she) scrolls it down.
The event is attached but is also defined directly on all WPF controls that implement IInputElement (seeing that Control implements IInputElement that is every control).
- I see what you're saying now. It doesn't appear that there is a separate MouseAction for wheel scroll up vs. down. Honestly, I'm not even sure if WheelClick does anything different than registering if the wheel is clicked or rotated. Maybe the WPF team can get that in for 3.5?
Actually you don't have to use the MouseWheelEvent. It is quite easy to create a new type of MouseGesture that allows you to set a gesture for moving the mouse wheel up or down. I have detailed how I did this on my blog. You can find my article called Creating a Customer InputGesture for Mouse Wheel Movements.
Cheers,
Caleb
All Replies
Try using the Mouse.MouseWheel event. The handler receives a MouseWheelEventArgs parameter that has a Delta property that is positive when the user scrolls the mousewheel up and negative when he (or she) scrolls it down.
The event is attached but is also defined directly on all WPF controls that implement IInputElement (seeing that Control implements IInputElement that is every control).
- Hi Mike, thanks for taking your time to response to my question.
As I indicated in the question, I know I can use MouseWheel event handler in code behind to react differently to mouse wheel rotation directions. What I wanted to know is whether it is possible to create different MouseBindings for different directions of rotation (or e.g. MouseBinding for only one direction), that is in XAML something like:
<Window.InputBindings>
<!-- this element is valid XAML element which unfortunately reacts to movement in any direction of mouse wheel -->
<MouseBinding MouseAction="WheelClick" Command="Cmd1" />
<!-- next two elements are invalid according to XAML schema, they are made up by me to illustrate there is something more needed to allow XAML authors to bind commands not only to key bindings and mouse clicks, but also to different directions of mouse wheel rotation -->
<MouseBinding MouseAction="WheelClick" Gesture="DirectionUp" Command="Cmd2" />
<MouseBinding MouseAction="WheelClick" Gesture="DirectionDown" Command="Cmd3" />
</Window.InputBindings>
WARNING: This just an illustrative, uncompilable, made up example.
Hopefully the question is more clear now.
Martin - I see what you're saying now. It doesn't appear that there is a separate MouseAction for wheel scroll up vs. down. Honestly, I'm not even sure if WheelClick does anything different than registering if the wheel is clicked or rotated. Maybe the WPF team can get that in for 3.5?
- It's got my vote. It'd nice.
Martin Actually you don't have to use the MouseWheelEvent. It is quite easy to create a new type of MouseGesture that allows you to set a gesture for moving the mouse wheel up or down. I have detailed how I did this on my blog. You can find my article called Creating a Customer InputGesture for Mouse Wheel Movements.
Cheers,
Caleb
This is the solution I've been looking for. Good work and thanks for sharing it!
- I created a suggestion to MSDN Connect:MouseAction enum should inculde WheelUp and WheelDown https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=512168Go vote if you care about this!


