1
votes

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
Are you saying when you hover the mouse over an empty area in the ScrollViewer, it doesn't scroll? Does it scroll when you hover over an image?Laith
No the other way. If I'm over an empty area in the ScrollViewer, it scrolls. If I'm over an Image it doesn't scroll.StefanH
try setting e.Handled = false; if you want that the parent scrolls.Hannes
Thanks a lot Hannes. This works perfectly. I will add an answer for that.StefanH

1 Answers

2
votes

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;
}