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

发布于 2024-11-06 01:57:26 字数 260 浏览 5 评论 0原文

嗯……差不多就是这样了。

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

编辑:

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

Um... that's pretty much it.

How can I animate a ScrollIntoView action in Silverlight 4?

Edits:

I know how to animated the scrolling. I just create my own DependencyProperty and scroll the ScrollViewer when it changes. What I need to do, is set up how much it should be changed? How do I calculate that? What does ScrollIntoView actually do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

<逆流佳人身旁 2024-11-13 01:57:26

来自反射镜:

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

From 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();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文