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
double NewVerticalOffset { get
double VerticalOffsetIncrement { get
double CurrentVerticalOffset { get
ScrollViewer ScrollViewer { get
double ViewportHeight { get
double VerticalOffset { get
double Intervals { get
public ScrollPage(ScrollViewer scrollViewer, double durationInSeconds)
{
ScrollViewer = scrollViewer
Intervals = durationInSeconds * 120
Timer = new DispatcherTimer()
Timer.Interval = TimeSpan.FromSeconds(1.0 / 120.0)
Timer.Tick += new EventHandler(Timer_Tick)
}
public void Up()
{
ViewportHeight = ScrollViewer.ViewportHeight
VerticalOffset = ScrollViewer.VerticalOffset
NewVerticalOffset = VerticalOffset - ViewportHeight + 75
if (NewVerticalOffset < 0)
{
NewVerticalOffset = 0
}
VerticalOffsetIncrement = (NewVerticalOffset - VerticalOffset) / Intervals
if (VerticalOffsetIncrement == 0.0)
{
return
}
CurrentVerticalOffset = VerticalOffset
Timer.Start()
}
public void Down()
{
ViewportHeight = ScrollViewer.ViewportHeight
VerticalOffset = ScrollViewer.VerticalOffset
NewVerticalOffset = VerticalOffset + ViewportHeight - 75
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)
}
}
}