How can I animate a ScrollIntoView action in Silverlight 4?
Um... that's pretty much it.
How can I animate a ScrollIntoView action in Silverlight 4?
Edits:
I know开发者_JS百科 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?
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();
}
}
}
精彩评论