/// <summary>
 /// Scroll a ScrollViewer horizontally by a given offset.
 ///
 /// </summary>
 /// <param name="viewer">The ScrollViewer.</param><param name="offset">The horizontal offset to scroll.</param>
 private static void ScrollByHorizontalOffset(ScrollViewer viewer, double offset)
 {
     Debug.Assert(viewer != null, "viewer should not be null!");
     offset += viewer.HorizontalOffset;
     offset  = ScrollViewerExtensions.CoerceHorizontalOffset(viewer, offset);
     viewer.ScrollToHorizontalOffset(offset);
 }
        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 ScrollViewer right by a page.
 ///
 /// </summary>
 /// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
 ///             </exception>
 public static void PageRight(this ScrollViewer viewer)
 {
     if (viewer == null)
     {
         throw new ArgumentNullException("viewer");
     }
     ScrollViewerExtensions.ScrollByHorizontalOffset(viewer, viewer.ViewportWidth);
 }
 /// <summary>
 /// Scroll the ScrollViewer down by a page.
 ///
 /// </summary>
 /// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
 ///             </exception>
 public static void PageDown(this ScrollViewer viewer)
 {
     if (viewer == null)
     {
         throw new ArgumentNullException("viewer");
     }
     ScrollViewerExtensions.ScrollByVerticalOffset(viewer, viewer.ViewportHeight);
 }
 /// <summary>
 /// Scroll the ScrollViewer right by a line.
 ///
 /// </summary>
 /// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
 ///             </exception>
 public static void LineRight(this ScrollViewer viewer)
 {
     if (viewer == null)
     {
         throw new ArgumentNullException("viewer");
     }
     ScrollViewerExtensions.ScrollByHorizontalOffset(viewer, 16.0);
 }
 /// <summary>
 /// Scroll the ScrollViewer up by a line.
 ///
 /// </summary>
 /// <param name="viewer">The ScrollViewer.</param><exception cref="T:System.ArgumentNullException"><paramref name="viewer"/> is null.
 ///             </exception>
 public static void LineUp(this ScrollViewer viewer)
 {
     if (viewer == null)
     {
         throw new ArgumentNullException("viewer");
     }
     ScrollViewerExtensions.ScrollByVerticalOffset(viewer, -16.0);
 }
 /// <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><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)
 {
     if (viewer == null)
     {
         throw new ArgumentNullException("viewer");
     }
     if (element == null)
     {
         throw new ArgumentNullException("element");
     }
     ScrollViewerExtensions.ScrollIntoView(viewer, element, 0.0, 0.0, (Duration)TimeSpan.Zero);
 }
 /// <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><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)
 {
     if (viewer == null)
     {
         throw new ArgumentNullException("viewer");
     }
     if (element == null)
     {
         throw new ArgumentNullException("element");
     }
     ScrollViewerExtensions.ScrollIntoView(viewer, element, 0.0, 112.0, TimeSpan.FromSeconds(0.25));
 }
        /// <summary>
        /// Handles the mouse wheel event.
        ///
        /// </summary>
        /// <param name="sender">The ScrollViewer.</param><param name="e">Event arguments.</param>
        private static void OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollViewer viewer = sender as ScrollViewer;

            Debug.Assert(viewer != null, "sender should be a non-null ScrollViewer!");
            Debug.Assert(e != null, "e should not be null!");
            if (e.Handled)
            {
                return;
            }
            double offset = ScrollViewerExtensions.CoerceVerticalOffset(viewer, viewer.VerticalOffset - (double)e.Delta);

            viewer.ScrollToVerticalOffset(offset);
            e.Handled = true;
        }
        /// <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();
            }
        }