Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 4 Jun 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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:
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)

Share

About the Author

fjparisIII
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 PinmemberMember 949638927-May-14 16:23 
QuestionEaseOut Animation? PinmemberMember 949638926-May-14 15:01 
QuestionBest solution I found was this one. Pinmembermdesgiw20-Apr-12 3:23 
GeneralReason for my vote of 5 Given me hint to implement the phone... PinmemberXITIJ_CODE_0424-Nov-11 3:25 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 5 Jun 2010
Article Copyright 2010 by fjparisIII
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid