Bubbling up / redirect event to parent ScrollViewer

放肆的年华 提交于 2019-12-11 08:26:14

问题


I hope someone have a hint/solution to solve this problem in a better and more performant way.

I'm writting an Windows UWP app with contains a ScrollViewer with a grid. In this grid I have two StackPanels embedded in a ScrollViewer.

Each StackPanel has a list of images in a vertical orientation. The StackPanel is as height as all images are together so there is no vertical scrollbar as it should be. On the other hand the images are wider then the StackPanel and the horizontal scrollbar is shown. I can scroll the StackPanels in the horizontal direction with the mouse. This works perfectly.

But if I'm over a StackPanel with the mouse I can not scroll the surrounding ScrollViewer with the mouse wheel in the vertical direction. The ScrollViewer around the StackPanels will consume the MouseWheel events and will not bubbling it up to the parent ScrollViewer.

Does someone know how I can bubbling up/redirect the MouseWheel event to the parent ScrollViewer?

I've found a not so good workaround for this. I've added an own UIElement.PointerWheelChangedEvent handler and move the outer ScrollViewer on my own, like this:

LeftScrollViewer.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(OnVerticalScrollEventHandled), true);

private void OnVerticalScrollEventHandled(object sender, PointerRoutedEventArgs e)
{
    ScrollViewPages.ChangeView(ScrollViewPages.HorizontalOffset, ScrollViewPages.VerticalOffset + (-1 * e.GetCurrentPoint(null).Properties.MouseWheelDelta), 1);
}

This works but not in a fast and fluid way. Has someone a better solution?


回答1:


For bubbling up an event from the child to the parent, add your own handler for the event and just set the handled property to false.

LeftScrollViewer.AddHandler(UIElement.PointerWheelChangedEvent, new PointerEventHandler(OnVerticalScrollEventHandled), true);

private void OnVerticalScrollEventHandled(object sender, PointerRoutedEventArgs e)
{
    e.Handled = false;
}


来源:https://stackoverflow.com/questions/43584409/bubbling-up-redirect-event-to-parent-scrollviewer

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