Click here to Skip to main content
15,884,099 members
Articles / Desktop Programming / WPF
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
4 Jun 2010CPOL2 min read 28K   4
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Paris Photo Tools
United States United States
I was faithfully married to MFC from 1994 to 2008, developing over a million lines of code for desktop applications during that time. Pockets of my employer at that time started flirting with WPF and I myself abandoned my longtime MFC wife and took up an affair with WPF, with which I've been infatuated ever since. Not that I wouldn't be willing to move back in with my wife for the right offer. But I still hold out the hope that WPF will be able to support me and all its other lovers over the long term.

Comments and Discussions

 
Questionproblem solved Pin
Member 949638927-May-14 15:23
Member 949638927-May-14 15:23 
QuestionEaseOut Animation? Pin
Member 949638926-May-14 14:01
Member 949638926-May-14 14:01 
QuestionBest solution I found was this one. Pin
mdesgiw20-Apr-12 2:23
mdesgiw20-Apr-12 2:23 
GeneralReason for my vote of 5 Given me hint to implement the phone... Pin
Xitij Thool24-Nov-11 2:25
Xitij Thool24-Nov-11 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.