horotab
horotab

Reputation: 683

prevent ScrollViewer to swallow MouseWheelEvent (Update: DevExpress LayoutControl is the reason)

I have an UserControl that contains a ScrollViewer which contains a Canvas. I have custom zoom logic, but if the ScrollViewer can scroll it swallows the mousewheel event completly. When the bottom is reached zooming works without problem.

    private void CanvasOverlayControl_PreviewMouseWheel(object sender, MouseWheelEventArgs e) {
        if(Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) {
            if(e.Delta > 0) {
                // Wheel up
                Zoom *= 1.15f;
            } else if(e.Delta < 0) {
                // Wheel down
                Zoom *= 0.85f;
            }
        }
    }

this line is in the constructor (code behind)

        PreviewMouseWheel += CanvasOverlayControl_PreviewMouseWheel;

How can I supress that behaviour?

Edit:

I found out DevExpress LayoutControl is causing this because it handles the scrolling of content. Is there any way to disable this?

Thanks in advance

Upvotes: 0

Views: 319

Answers (1)

grek40
grek40

Reputation: 13448

Lets start with the basics... ensure that the Canvas is actually getting any mouse events. It needs some background to allow hit testing

<Canvas Background="Transparent"

For me, the PreviewMouseWheel of a canvas is hit and I can disable scrolling of the parent by setting e.Handled = true;.

Edit: the sample project:

<Window x:Class="WpfTests_2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        ...
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="grid1">
        <ScrollViewer>
            <Canvas Background="Green" PreviewMouseWheel="Canvas_PreviewMouseWheel" Width="300" Height="1200"/>
        </ScrollViewer>
    </Grid>
</Window>

Code behind

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Canvas_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        e.Handled = true;
    }
}

Observed result: when the mouse is over the green area of the canvas, no scrolling on mouse wheel. When the mouse is outside the canvas, the scrollviewer scrolls.

Upvotes: 1

Related Questions