简体   繁体   中英

Scrolling with touch not working in WPF

I have a WPF application and over a part of it I had a card which displayed Javascript. My problem was that I wasn't able to scroll with the mouse over the Javascript card, because it was swallowing the events. I solved that by hooking into the PreviewMouseWheel event (described here) sent by UIElement :

protected void AddListeners(BorderedCanvas borderedCanvas)
{
    borderedCanvas.PreviewMouseWheel += PreviewMouseWheel;
}

protected void RemoveListeners(BorderedCanvas borderedCanvas)
{
    borderedCanvas.PreviewMouseWheel -= PreviewMouseWheel;
}

protected static void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (!e.Handled)
    {
        e.Handled = true;

        var eventArg =
            new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
            {
                RoutedEvent = UIElement.MouseWheelEvent,
                Source = sender
            };

        var parent = VisualTreeHelper.GetParent((UIElement)sender) as UIElement;
        parent?.RaiseEvent(eventArg);
    }
}

Now, scrolling with the mouse works perfectly fine.

However, scrolling with touch doesn't work.

So, I modified AddListeners :

    protected void AddListeners(BorderedCanvas borderedCanvas)
    {
        borderedCanvas.PreviewMouseWheel += PreviewMouseWheel;
        borderedCanvas.PreviewTouchDown += new EventHandler<TouchEventArgs>(OnTouchDown);
        borderedCanvas.PreviewTouchMove += new EventHandler<TouchEventArgs>(OnTouchMove);
    }

And I added these methods:

    protected static void OnTouchDown(object sender, TouchEventArgs e)
    {
        if (!e.Handled)
        {
            e.Handled = true;

            var eventArg =
                new TouchEventArgs(e.TouchDevice, e.Timestamp)
                {
                    RoutedEvent = UIElement.TouchDownEvent,
                    Source = sender
                };

            var parent = VisualTreeHelper.GetParent((UIElement)sender) as UIElement;
            parent?.RaiseEvent(eventArg);
        }
    }

    protected static void OnTouchMove(object sender, TouchEventArgs e)
    {
        if (!e.Handled)
        {
            e.Handled = true;

            var eventArg =
                new TouchEventArgs(e.TouchDevice, e.Timestamp)
                {
                    RoutedEvent = UIElement.TouchMoveEvent,
                    Source = sender
                };

            var parent = VisualTreeHelper.GetParent((UIElement)sender) as UIElement;
            parent?.RaiseEvent(eventArg);
        }
    }

However, scrolling with touch still doesn't work.

Any idea what I did wrong?

To me it seems that I treated the mouse scroll and the touch scroll in a similar way, but the first works and the second doesn't.

You need to set the ScrollViewer.PanningMode for it to work with the touch.

<ScrollViewer PanningMode="Both"/>

The default value for the PanningMode is None.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM