public static void ScrollToBeginnig(this ScrollViewer viewer, Duration duration) { if (viewer == null) { throw new ArgumentNullException("viewer"); } Storyboard storyboard = new Storyboard(); ScrollViewerExtensions.SetVerticalOffset(viewer, viewer.VerticalOffset); ScrollViewerExtensions.SetHorizontalOffset(viewer, viewer.HorizontalOffset); DoubleAnimation doubleAnimation1 = new DoubleAnimation(); doubleAnimation1.To = new double?(0.0); doubleAnimation1.Duration = duration; DoubleAnimation doubleAnimation2 = doubleAnimation1; DoubleAnimation doubleAnimation3 = new DoubleAnimation(); doubleAnimation3.To = new double?(0.0); doubleAnimation3.Duration = duration; DoubleAnimation doubleAnimation4 = doubleAnimation3; Storyboard.SetTarget((Timeline)doubleAnimation2, (DependencyObject)viewer); Storyboard.SetTarget((Timeline)doubleAnimation4, (DependencyObject)viewer); Storyboard.SetTargetProperty((Timeline)doubleAnimation4, new PropertyPath((object)ScrollViewerExtensions.HorizontalOffsetProperty)); Storyboard.SetTargetProperty((Timeline)doubleAnimation2, new PropertyPath((object)ScrollViewerExtensions.VerticalOffsetProperty)); storyboard.Children.Add((Timeline)doubleAnimation2); storyboard.Children.Add((Timeline)doubleAnimation4); storyboard.Begin(); }
/// <summary> /// Scroll the desired element into the ScrollViewer's viewport. /// /// </summary> /// <param name="viewer">The ScrollViewer.</param><param name="element">The element to scroll into view.</param><param name="horizontalMargin">The margin to add on the left or right. /// </param><param name="verticalMargin">The margin to add on the top or bottom. /// </param><param name="duration">The duration of the animation.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null. /// </exception><exception cref="T:System.ArgumentNullException"><paramref name="element"/> is null. /// </exception> public static void ScrollIntoView(this ScrollViewer viewer, FrameworkElement element, double horizontalMargin, double verticalMargin, Duration duration) { if (viewer == null) { throw new ArgumentNullException("viewer"); } if (element == null) { throw new ArgumentNullException("element"); } Rect?boundsRelativeTo = VisualTreeExtensions.GetBoundsRelativeTo(element, (UIElement)viewer); if (!boundsRelativeTo.HasValue) { return; } double verticalOffset = viewer.VerticalOffset; double num1 = 0.0; double viewportHeight = viewer.ViewportHeight; double num2 = boundsRelativeTo.Value.Bottom + verticalMargin; if (viewportHeight < num2) { num1 = num2 - viewportHeight; verticalOffset += num1; } double num3 = boundsRelativeTo.Value.Top - verticalMargin; if (num3 - num1 < 0.0) { verticalOffset -= num1 - num3; } double horizontalOffset = viewer.HorizontalOffset; double num4 = 0.0; double viewportWidth = viewer.ViewportWidth; double num5 = boundsRelativeTo.Value.Right + horizontalMargin; if (viewportWidth < num5) { num4 = num5 - viewportWidth; horizontalOffset += num4; } double num6 = boundsRelativeTo.Value.Left - horizontalMargin; if (num6 - num4 < 0.0) { horizontalOffset -= num4 - num6; } if (duration == (Duration)TimeSpan.Zero) { viewer.ScrollToVerticalOffset(verticalOffset); viewer.ScrollToHorizontalOffset(horizontalOffset); } else { Storyboard storyboard = new Storyboard(); ScrollViewerExtensions.SetVerticalOffset(viewer, viewer.VerticalOffset); ScrollViewerExtensions.SetHorizontalOffset(viewer, viewer.HorizontalOffset); DoubleAnimation doubleAnimation1 = new DoubleAnimation(); doubleAnimation1.To = new double?(verticalOffset); doubleAnimation1.Duration = duration; DoubleAnimation doubleAnimation2 = doubleAnimation1; DoubleAnimation doubleAnimation3 = new DoubleAnimation(); doubleAnimation3.To = new double?(verticalOffset); doubleAnimation3.Duration = duration; DoubleAnimation doubleAnimation4 = doubleAnimation3; Storyboard.SetTarget((Timeline)doubleAnimation2, (DependencyObject)viewer); Storyboard.SetTarget((Timeline)doubleAnimation4, (DependencyObject)viewer); Storyboard.SetTargetProperty((Timeline)doubleAnimation4, new PropertyPath((object)ScrollViewerExtensions.HorizontalOffsetProperty)); Storyboard.SetTargetProperty((Timeline)doubleAnimation2, new PropertyPath((object)ScrollViewerExtensions.VerticalOffsetProperty)); storyboard.Children.Add((Timeline)doubleAnimation2); storyboard.Children.Add((Timeline)doubleAnimation4); storyboard.Begin(); } }