static void scroll_ScrollChanged(object sender, ScrollChangedEventArgs e) { try { ZoomingScrollViewer scroll = sender as ZoomingScrollViewer; if (scroll._isRelativeSet) { if (e.ExtentWidthChange != 0 || e.ExtentHeightChange != 0) { scroll.ScrollToHorizontalOffset(CalculateOffset(e.ExtentWidth, e.ViewportWidth, scroll.ScrollableWidth, scroll.relX)); scroll.ScrollToVerticalOffset(CalculateOffset(e.ExtentHeight, e.ViewportHeight, scroll.ScrollableHeight, scroll.relY)); } else { scroll.relX = (e.HorizontalOffset + 0.5 * e.ViewportWidth) / e.ExtentWidth; scroll.relY = (e.VerticalOffset + 0.5 * e.ViewportHeight) / e.ExtentHeight; } } else { scroll.relX = (e.HorizontalOffset + 0.5 * e.ViewportWidth) / e.ExtentWidth; scroll.relY = (e.VerticalOffset + 0.5 * e.ViewportHeight) / e.ExtentHeight; scroll._isRelativeSet = true; } scroll.OnScrollChanged(); } catch { } }
static void scrollSelectedZoom_ScrollChanged(object sender, ScrollChangedEventArgs e) { try { ZoomingScrollViewer scroll = sender as ZoomingScrollViewer; if (e.ExtentWidthChange != 0 || e.ExtentHeightChange != 0) { scroll.ScrollToSelectedCenterPoint(); } scroll.OnScrollChanged(); } catch { } }
private static void OnKeepInCenterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ZoomingScrollViewer scroll = d as ZoomingScrollViewer; if (scroll != null) { if ((bool)e.NewValue) { scroll.ScrollChanged += scroll_ScrollChanged; scroll.ScrollChanged -= scrollSelectedZoom_ScrollChanged; scroll._isRelativeSet = false; } else { scroll.ScrollChanged -= scroll_ScrollChanged; scroll.ScrollChanged += scrollSelectedZoom_ScrollChanged; } } }