In my last post I described a class that wraps the ScrollViewer control to enable multi-touch panning in it.
Well, it’s not exactly the right way to do it. You should be receiving the pan messages in the Window, then finding the scroll viewer where the window was touched using a Hit Test, and scroll that ScrollViewer.
Code Snippet
- private void Page2_Loaded(object sender, RoutedEventArgs e)
- {
- // Get our items for the itemscontrols
- this.GetItems();
-
- // Check for multi-touch capabilities
- if (Windows7.Multitouch.Handler.DigitizerCapabilities.IsMultiTouchReady)
- {
- // Enable stylus events
- Windows7.Multitouch.WPF.Factory.EnableStylusEvents(this);
- // Add the gesture handler
- this.GestureHandler = Windows7.Multitouch.WPF.Factory.CreateGestureHandler(Window.GetWindow(this));
- // Attach the handler to the method
- this.GestureHandler.Pan += new EventHandler<Windows7.Multitouch.GestureEventArgs>(this.GestureHandler_Pan);
- }
- }
-
-
- private void GestureHandler_Pan(object sender, Windows7.Multitouch.GestureEventArgs e)
- {
- this.panTranslation = e.PanTranslation;
-
- PointHitTestParameters hitTestParameters = new PointHitTestParameters(Windows7.Multitouch.WPF.PointUtil.ToDrawingPointF(e.Center));
- VisualTreeHelper.HitTest(this, null, new HitTestResultCallback(this.HitTestResult), hitTestParameters);
- }
-
-
- public HitTestResultBehavior HitTestResult(HitTestResult rawresult)
- {
- ScrollViewer sv = null;
- try
- {
- sv = (ScrollViewer)rawresult.VisualHit;
- }
- catch
- {
- }
-
- // If we have a reference to the scrollviewer, then we scroll
- if (sv != null)
- {
- // Make the Scroller scroll
- sv.ScrollToVerticalOffset(sv.VerticalOffset - this.panTranslation.Height);
-
- // Stop trying to find more controls in the visual tree
- return HitTestResultBehavior.Stop;
-
- }
- else
- {
- // Continue trying to find more controls in the visual tree
- return HitTestResultBehavior.Continue;
- }
-
- }
When you are tracking multi-touch in a specific control, the touchs register at the window level, so even if you are not touching in the ScrollViewer, it still will pan it.
So the code was moved to the window , and uses the standard <ScrollViewer>.
Download the example project in C# and VB.NET
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5