如何在Silverlight 4中为ScrollIntoView动作设置动画?

时间:2011-05-10 15:22:15

标签: silverlight animation scrollviewer

嗯......那就是它。

如何在Silverlight 4中为ScrollIntoView动作设置动画?

编辑:

我知道如何动画滚动。我只是创建自己的DependencyProperty并在更改时滚动ScrollViewer。我需要做的是设置应该改变多少?我该如何计算? ScrollIntoView实际上做了什么?

1 个答案:

答案 0 :(得分:1)

来自Reflector:

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();
        }
    }
}