Как я могу анимировать действие ScrollIntoView в Silverlight 4? - PullRequest
0 голосов
/ 10 мая 2011

Гм ... вот и все.

Как анимировать действие ScrollIntoView в Silverlight 4?

Правки:

Iзнать, как анимировать прокрутку.Я просто создаю свой собственный DependencyProperty и прокручиваю ScrollViewer, когда он меняется.Что мне нужно сделать, это настроить, насколько это должно быть изменено?Как рассчитать это?Что на самом деле делает ScrollIntoView

1 Ответ

1 голос
/ 10 мая 2011

От отражателя:

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? itemRect = element.GetBoundsRelativeTo(viewer);
    if (itemRect.HasValue)
    {
        double verticalOffset = viewer.VerticalOffset;
        double verticalDelta = 0.0;
        double hostBottom = viewer.ViewportHeight;
        double itemBottom = itemRect.Value.Bottom + verticalMargin;
        if (hostBottom < itemBottom)
        {
            verticalDelta = itemBottom - hostBottom;
            verticalOffset += verticalDelta;
        }
        double itemTop = itemRect.Value.Top - verticalMargin;
        if ((itemTop - verticalDelta) < 0.0)
        {
            verticalOffset -= verticalDelta - itemTop;
        }
        double horizontalOffset = viewer.HorizontalOffset;
        double horizontalDelta = 0.0;
        double hostRight = viewer.ViewportWidth;
        double itemRight = itemRect.Value.Right + horizontalMargin;
        if (hostRight < itemRight)
        {
            horizontalDelta = itemRight - hostRight;
            horizontalOffset += horizontalDelta;
        }
        double itemLeft = itemRect.Value.Left - horizontalMargin;
        if ((itemLeft - horizontalDelta) < 0.0)
        {
            horizontalOffset -= horizontalDelta - itemLeft;
        }
        if (duration == TimeSpan.Zero)
        {
            viewer.ScrollToVerticalOffset(verticalOffset);
            viewer.ScrollToHorizontalOffset(horizontalOffset);
        }
        else
        {
            Storyboard storyboard = new Storyboard();
            SetVerticalOffset(viewer, viewer.VerticalOffset);
            SetHorizontalOffset(viewer, viewer.HorizontalOffset);
            DoubleAnimation verticalOffsetAnimation = new DoubleAnimation {
                To = new double?(verticalOffset),
                Duration = duration
            };
            DoubleAnimation horizontalOffsetAnimation = new DoubleAnimation {
                To = new double?(verticalOffset),
                Duration = duration
            };
            Storyboard.SetTarget(verticalOffsetAnimation, viewer);
            Storyboard.SetTarget(horizontalOffsetAnimation, viewer);
            Storyboard.SetTargetProperty(horizontalOffsetAnimation, new PropertyPath(HorizontalOffsetProperty));
            Storyboard.SetTargetProperty(verticalOffsetAnimation, new PropertyPath(VerticalOffsetProperty));
            storyboard.Children.Add(verticalOffsetAnimation);
            storyboard.Children.Add(horizontalOffsetAnimation);
            storyboard.Begin();
        }
    }
}
...