Wednesday, December 16, 2009

Continue scrolling while mouseover - Silverlight

Context: While mouse cursor is over an object (i.e. button, image, canvas), a function should continue to occur. This is useful for continuous horizontal scrolling of menu items.

The idea is to declare two DispatcherTimer objects (one for each direction of scrolling). You can set the interval of the timer objects accordingly. The Tick property defines the method that the timer calls at each interval.


private System.Windows.Threading.DispatcherTimer _ScrollUpDispatcherTimer;
private System.Windows.Threading.DispatcherTimer _ScrolDownDispatcherTimer;

private void bNext_MouseEnter(object sender, MouseEventArgs e)
{
_ScrolDownDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
_ScrolDownDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
_ScrolDownDispatcherTimer.Tick += new EventHandler(ScrollDown_Each_Tick);
_ScrolDownDispatcherTimer.Start();
}
private void bPrev_MouseEnter(object sender, MouseEventArgs e)
{
_ScrollUpDispatcherTimer = new System.Windows.Threading.DispatcherTimer();
_ScrollUpDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);
_ScrollUpDispatcherTimer.Tick += new EventHandler(ScrollUp_Each_Tick);
_ScrollUpDispatcherTimer.Start();
}

public void ScrollUp_Each_Tick(object o, EventArgs sender)
{
//scroll-up function
}
public void ScrollDown_Each_Tick(object o, EventArgs sender)
{
//scroll-down function
}

private void bPrev_MouseLeave(object sender, MouseEventArgs e)
{
_ScrollUpDispatcherTimer.Stop();
}
private void bNext_MouseLeave(object sender, MouseEventArgs e)
{
_ScrolDownDispatcherTimer.Stop();
}

Remember to call the respective stop() functions in the scrolling functions if there is nothing more to scroll.

No comments:

Post a Comment