Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to animate vertical scrolling of a Silverlight ScrollViewer a page at a time

0.00/5 (No votes)
4 Jun 2010 1  
Cannot use Storyboard because ScrollView.VerticalOffset property is read-only. Must use old fashioned DispatchTimer.
This Tip builds on the work that was done for the following Tip: http://www.codeproject.com/Tips/85359/How-to-animate-vertical-centering-of-a-Silverlight.aspx
Read that tip for background.

In this case, the animated scroll is a "page" worth of content within the viewport of a ScrollViewer. Silverlight 4 (at least; I'm not sure about 3) supports the PageUp and PageDown key within a ScrollViewer. Those keys scroll the contents of the ScrollViewer a viewport's worth of data per keystroke.

There are two problems with what these keystrokes do. First, the action happens instantaneously and it's not obvious what actually happened. It needs to be animated so that the user can actually see what happened, namely that the ScrollViewer scrolled up or down. Second, the obscurity of what happened is made worse in that the entire viewport is scrolled up and down with each keystroke. Therefore the user has no context remaining to see what actually did happen.

My solution to these problems is thus two-fold. First, the scrolling operation is animated and so the user can actually observe what is happening. Experimentally, I've found that the animation seems best when it occurs over a period of 0.35 seconds. That way, the user can see that a scroll is taking place but he doesn't have to wait around "a long time" for the operation to complete.

In examining the code below, you will also see that the implementation of the animation is by "brute force," using the DispatcherTimer rather than Silverlight Animation and Storyboard classes. As explained in the original article, this was necessary because the ScrollViewer.VerticalOffset property is read-only, and so using the built-in Silverlight animation classes is impossible.

The second problem solved by my "Tip or Trick" is that the scroll within the viewport is only partial, not 100%. Therefore the content from one scroll to the next overlaps, by 75 device-independent units to be exact. I found that this amount provides enough context to be obvious without wasting too much of an opportunity to do a lot of scrolling with a single mouse click.

The class used to do all this I named ScrollPage and is similar to the class I posted previously, in taking the same two parameters in the constructor: a ScrollViewer element and an animation duration in seconds. It also has two public member functions, Up() and Down(), neither of which takes any parameters.

To make use of the class, I simply have up and down arrows beneath the ScrollViewer with Click events that are associated with each arrow. The events create instances of ScrollPage and then call the Up() or Down() member functions. Usage thus could not be simpler.

Once again, you can see this class in action by visiting my Website here: http://powerphototools.com Simply bring up the product page or any of the tutorials and work the arrow buttons at the bottom of each page.

Here then is the source:
MSIL
public class ScrollPage
{
    DispatcherTimer Timer { get; set; }
    double NewVerticalOffset { get; set; }
    double VerticalOffsetIncrement { get; set; }
    double CurrentVerticalOffset { get; set; }
    ScrollViewer ScrollViewer { get; set; }
    double ViewportHeight { get; set; }
    double VerticalOffset { get; set; }
    double Intervals { get; set; }

    public ScrollPage(ScrollViewer scrollViewer, double durationInSeconds)
    {
        ScrollViewer = scrollViewer;
        Intervals = durationInSeconds * 120; // number of timer Intervals
        Timer = new DispatcherTimer();
        Timer.Interval = TimeSpan.FromSeconds(1.0 / 120.0);
        Timer.Tick += new EventHandler(Timer_Tick);
    }

    // This member function scrolls the ScrollViewer up ViewportHeight - 75
    // device-independent units so that there is overlap between one view and the next.
    public void Up()
    {
        // The user can change this between expansions/collapses.
        ViewportHeight = ScrollViewer.ViewportHeight;

        // The user can change this by moving the thumb control.
        // Equivalent to the data type Animation.From property.
        VerticalOffset = ScrollViewer.VerticalOffset;

        // Equivalent to the data type Animation.To property.
        NewVerticalOffset = VerticalOffset - ViewportHeight + 75;

        // We don't want to try to scroll out of the ScrollViewer.
        if (NewVerticalOffset < 0)
        {
            NewVerticalOffset = 0;
        }
        VerticalOffsetIncrement = (NewVerticalOffset - VerticalOffset) / Intervals;
        if (VerticalOffsetIncrement == 0.0)
        {
            return;
        }
        CurrentVerticalOffset = VerticalOffset;
        Timer.Start();
    }

    // This member function scrolls the ScrollViewer down ViewportHeight - 75
    // device-independent units so that there is overlap between one view and the next.
    public void Down()
    {
        // The user can change this between expansions/collapses.
        ViewportHeight = ScrollViewer.ViewportHeight;

        // The user can change this by moving the thumb control.
        // Equivalent to the data type Animation.From property.
        VerticalOffset = ScrollViewer.VerticalOffset;

        // Equivalent to the data type Animation.To property.
        NewVerticalOffset = VerticalOffset + ViewportHeight - 75;

        // We don't want to try to scroll out of the ScrollViewer.
        if (NewVerticalOffset > ScrollViewer.ExtentHeight)
        {
            NewVerticalOffset = ScrollViewer.ExtentHeight;
        }
        VerticalOffsetIncrement = (NewVerticalOffset - VerticalOffset) / Intervals;
        if (VerticalOffsetIncrement == 0.0)
        {
            return;
        }
        CurrentVerticalOffset = VerticalOffset;
        Timer.Start();
    }

    void Timer_Tick(object sender, EventArgs e)
    {
        CurrentVerticalOffset += VerticalOffsetIncrement;
        if (VerticalOffsetIncrement > 0 && CurrentVerticalOffset > NewVerticalOffset ||
            VerticalOffsetIncrement < 0 && NewVerticalOffset > CurrentVerticalOffset)
        {
            Timer.Stop();
        }
        else
        {
            ScrollViewer.ScrollToVerticalOffset(CurrentVerticalOffset);
        }
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here