private void ZoomSliderOnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { ZoomTransform.ScaleX = e.NewValue; ZoomTransform.ScaleY = e.NewValue; var centerOfViewport = new Point(ZoomViewer.ViewportWidth/2, ZoomViewer.ViewportHeight/2); _lastCenterPositionOnTarget = ZoomViewer.TranslatePoint(centerOfViewport, ZoomGrid); }
private void ZoomViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) { if (e.ExtentHeightChange != 0 || e.ExtentWidthChange != 0) { Point? targetBefore = null; Point? targetNow = null; if (!_lastMousePositionOnTarget.HasValue) { if (_lastCenterPositionOnTarget.HasValue) { var centerOfViewport = new Point(ZoomViewer.ViewportWidth/2, ZoomViewer.ViewportHeight/2); Point centerOfTargetNow = ZoomViewer.TranslatePoint(centerOfViewport, ZoomGrid); targetBefore = _lastCenterPositionOnTarget; targetNow = centerOfTargetNow; } } else { targetBefore = _lastMousePositionOnTarget; targetNow = Mouse.GetPosition(ZoomGrid); _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/ZoomGrid.Width; double multiplicatorY = e.ExtentHeight/ZoomGrid.Height; double newOffsetX = ZoomViewer.HorizontalOffset - dXInTargetPixels*multiplicatorX; double newOffsetY = ZoomViewer.VerticalOffset - dYInTargetPixels*multiplicatorY; if (double.IsNaN(newOffsetX) || double.IsNaN(newOffsetY)) { return; } ZoomViewer.ScrollToHorizontalOffset(newOffsetX); ZoomViewer.ScrollToVerticalOffset(newOffsetY); } } }