2012-09-28 19 views
5

我创建了一个使用C#/ XAML的Windows 8商店应用程序。我的界面包含一个滚动列表,该列表使用ScrollViewer呈现。我希望能够处理列表中元素的操纵事件,但是,将ManipulationMode设置为除列表元素上的其他任何内容都会导致我的列表不再滚动。ScrollViewer和处理子元素上的操作事件

这里是UI的简化版本:

<ScrollViewer> 
    <Border/> <!-- these contain child content --> 
    <Border/> 
    <Border/> 
    <!-- Set ManipulationMode on an element in order to receive manipulation events --> 
    <!-- This causes the scroll viewer to stop working! --> 
    <Border ManipulationMode="All" 
      ManipulationDelta="..."/> 
    <Border/> 
    <Border/> 
</ScrollViewer> 

我明白的WinRT ScrollViewer使用的System出于性能方面的特殊ManipulationMode,但我想有一个垂直滚动列表,包含元素响应水平操纵/手势。任何人都可以想到一个创造性的解决方法,这将使这成为可能吗?

回答

8

我所做的是我在ScrollViewer的顶部放置了一个透明的矩形,并在那里处理操作。当我发现操作应该滚动ScrollViewer - 我使用ScrollToHorizo​​ntal/VerticalOffset()方法滚动ScrollViewer。在ManipulationStarted上,我还使用VisualTreeHelper.FindElementsInHostCoordinates来检查我可以操作的项目,然后根据各种条件决定是否操作该项目。不过,这是相当多的自定义代码。当用户试图拖动比最小/最大偏移更远的值以模仿ScrollViewer的默认行为,处理鼠标滚轮等时,您还需要更新ScrollViewer中的ScrollContentPresenter的RenderTransform。当然,您无法处理任何内容。不幸的是我找不到更好的方式,而且如果有人找到一个,我很感兴趣。

编辑*我想到的另一个解决方案,当试图回答another similar question是使用另一个ScrollViewer作为子项目,并使用其ViewChanged事件,而不是操纵事件。

EDIT 2 *

而且与Windows 8.1你ManipulationModes.System与其他模式相结合应该让你处理ScrollViewer的内部操作。然后,您可以在操作元素上调用CancelDirectManipulations(),一旦您希望其父ScrollViewers停止处理pan &缩放操作。

+0

哇 - 这是很多工作。所以你基本上照顾自己滚动ScrollViewer吗?这是否意味着您还必须执行自己的惯性计算才能自然滚动?或者当你改变垂直偏移时它会自动执行此操作吗? – ColinE

+1

木星中的操作默认情况下启用了内置惯性,因此您可以获得。如果由于惯性而发生滚动,您只需确保不要在列表末尾挤压ScrollContentPresenter。操作通常非常流畅,但您可以使用ScrollViewer动画,如[WinRT XAML Toolkit](http://bit.ly/WinRTXamlToolkit)中的动画来平滑使用滚轮时的滚动。顺便说一句,我忘了提及你可能还想处理设置重点项目时,当你点击覆盖... :) –

+1

@ColinE检查我的答案[这个问题](http:// stackoverflow。com/questions/14153038/how-to-allow-manipulation-within-listview-gridview-item-controls-while-allow/14161596#14161596)另一种解决方案。 –

10

它可能很长时间,但没有找到任何好的解决方案。我刚刚达到了我想要的要求。

public MovableGrid() 
     { 
      ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.System; 
      AddHandler(ManipulationDeltaEvent, new ManipulationDeltaEventHandler(UIElement_OnManipulationDelta), true); 
      AddHandler(ManipulationCompletedEvent, new ManipulationCompletedEventHandler(UIElement_OnManipulationCompleted), true); 
     } 

我希望我的MovableGrid在X轴移动,我有我想用的ScrollViewer滚动MovableGrids的名单。这足以做到这一点。

+0

谢谢!这正是我需要的! – Pam

相关问题