C# 如何使操纵事件在包含ScrollViewer子元素的元素上工作
假设我有以下XAML:C# 如何使操纵事件在包含ScrollViewer子元素的元素上工作,c#,xaml,uwp,windows-runtime,winrt-xaml,C#,Xaml,Uwp,Windows Runtime,Winrt Xaml,假设我有以下XAML: <Grid ManipulationMode="TranslateY" ManipulationDelta="Grid_ManipulationDelta"> <ScrollViewer HorizontalScrollMode="Enabled" VerticalScrollMode="Disabled" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="H
<Grid ManipulationMode="TranslateY" ManipulationDelta="Grid_ManipulationDelta">
<ScrollViewer HorizontalScrollMode="Enabled" VerticalScrollMode="Disabled" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden">
<StackPanel Orientation="Horizontal">
<Rectangle Width="100" Height="100" Fill="Red" />
<Rectangle Width="100" Height="100" Fill="Blue" />
<Rectangle Width="100" Height="100" Fill="Red" />
<Rectangle Width="100" Height="100" Fill="Blue" />
<Rectangle Width="100" Height="100" Fill="Red" />
<Rectangle Width="100" Height="100" Fill="Blue" />
<Rectangle Width="100" Height="100" Fill="Red" />
<Rectangle Width="100" Height="100" Fill="Blue" />
</StackPanel>
</ScrollViewer>
</Grid>
(StackPanel只是任意内容。)
我希望网格只处理TranslateY操作事件,而ScrollViewer只水平滚动;但是,即使操作是垂直的而不是水平的,ScrollViewer也优先。网格从不接收操纵增量事件,除非我在ScrollViewer上禁用滚动,这显然不是我想要的
这是否与ScrollViewer直接操纵事件的“神奇”行为有关?我也尝试过不同的“rails”设置组合(即,在ScrollViewer上将Grid的操纵模式设置为TranslateRailsY,并将IsHorizontalRailEnabled设置为true),但仍然不起作用。ScrollViewer将操纵事件标记为已处理,这会导致操纵事件不会冒泡到网格上。你必须改变这种行为
您可以从ScrollViewer订阅事件。关于该事件的文档应该让您开始。您是对的,
ScrollViewer
的父元素不会接收操作事件,因为它们被ScrollViewer
的直接操作事件阻止
但是,只要操作模式
包括系统
模式,ScrollViewer
中的元素就可以接收它们
在您的特定情况下,您需要监控StackPanel
上的operationdelta
事件,并将其operationmode
设置为TranslateY,System
另外,不要忘记为您的StackPanel
提供一个透明的背景以接收事件
<StackPanel Background="Transparent" ManipulationMode="TranslateY,System" ManipulationDelta="StackPanel_ManipulationDelta">
ScrollViewer
在UWP中是密封的。