void OnSliderValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e) { var centerOfViewport = new Point(PlanScrollViewer.ViewportWidth / 2, PlanScrollViewer.ViewportHeight / 2); _lastCenterPositionOnTarget = PlanScrollViewer.TranslatePoint(centerOfViewport, PlanGrid); ShowAllButton.Visibility = e.NewValue == 1 ? Visibility.Hidden : Visibility.Visible; }
void OnScrollViewerScrollChanged(object sender, ScrollChangedEventArgs e) { if (_animating || (e.ExtentHeightChange == 0 && e.ExtentWidthChange == 0)) { return; } Point?targetBefore = null; Point?targetNow = null; if (!_lastMousePositionOnTarget.HasValue) { if (_lastCenterPositionOnTarget.HasValue) { var centerOfViewport = new Point(PlanScrollViewer.ViewportWidth / 2, PlanScrollViewer.ViewportHeight / 2); Point centerOfTargetNow = PlanScrollViewer.TranslatePoint(centerOfViewport, PlanGrid); targetBefore = _lastCenterPositionOnTarget; targetNow = centerOfTargetNow; } } else { targetBefore = _lastMousePositionOnTarget; targetNow = Mouse.GetPosition(PlanGrid); _lastMousePositionOnTarget = null; } if (targetBefore.HasValue) { double dXInTargetPixels = targetNow.Value.X - targetBefore.Value.X; double dYInTargetPixels = targetNow.Value.Y - targetBefore.Value.Y; double multiplicatorX = e.ExtentWidth / PlanGrid.ActualWidth; double multiplicatorY = e.ExtentHeight / PlanGrid.ActualHeight; double newOffsetX = PlanScrollViewer.HorizontalOffset - dXInTargetPixels * multiplicatorX; double newOffsetY = PlanScrollViewer.VerticalOffset - dYInTargetPixels * multiplicatorY; if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY)) { return; } PlanScrollViewer.ScrollToHorizontalOffset(newOffsetX); PlanScrollViewer.ScrollToVerticalOffset(newOffsetY); } }