问题描述
我希望有人能提供一个提示/解决方案,以更好,更高效的方式解决此问题.
I hope someone have a hint/solution to solve this problem in a better and more performant way.
我正在编写一个Windows UWP应用程序,其中包含带有网格的ScrollViewer.在此网格中,我在ScrollViewer中嵌入了两个StackPanel.
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.
每个StackPanel都有一个垂直方向的图像列表. StackPanel的高度与所有图像的高度一样,因此没有垂直滚动条.另一方面,图像要比StackPanel和水平滚动条宽.我可以用鼠标在水平方向上滚动StackPanels.效果很好.
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.
但是,如果我用鼠标在StackPanel上,则无法使用鼠标滚轮在垂直方向上滚动周围的ScrollViewer.围绕StackPanel的ScrollViewer将使用MouseWheel事件,并且不会将其冒泡到父级ScrollViewer.
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.
有人知道我如何冒泡/重定向MouseWheel事件到父级ScrollViewer吗?
Does someone know how I can bubbling up/redirect the MouseWheel event to the parent ScrollViewer?
我找到了一个不太好的解决方法.我添加了一个自己的UIElement.PointerWheelChangedEvent处理程序,并自己移动了外部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?
推荐答案
要使事件从子级向父级冒泡,请为该事件添加您自己的处理程序,然后将handleded属性设置为false.
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;
}
这篇关于冒泡/重定向事件到父级ScrollViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!