views:

524

answers:

3

I am trying to create a custom scrollbar and am using images as button.

For now a simple

I can handle the MouseLeftButtonDown and Up event just fine but what I'd like to do is while its held down, every so many millisecond trigger an event is fired.

I tried something like this but it isn't quite working. Suggestions?

public delegate void Changed(RangeScrollButtonControl sender, int value);
public event Changed OnChanged;
private System.Threading.Timer Timer;

private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    this.Timer = new System.Threading.Timer(Timer_Callback, null, 0, 100);
}

private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    this.Timer = null;
}

private void Timer_Callback(object sender)
{
    if (this.OnChanged != null) 
    {
        this.OnChanged(this, 1);
    }
}
A: 

Which piece "isn't quite working" ?

Also, could you restyle or retemplate Silverlght's scrollbar similar to what is seen in this blog post to get what you need?

loarabia
It appeared the Timer object isn't thread-safe and I was running into cross-thread exceptions. The solution was to use DispatchTimer and it's working fine now.
Gautam
A: 

I would use a Storyboard as a timer. Something like:

Then you can do a MouseSTB.Begin. Once the Storyboard is finished you can catch it in the MouseSTB.Completeed Event. In that event you can do what ever you need to do and then just start it over again. It can easilly be controled by setting some flags on the mouseover, mouseenter and mouseleave events. I use these timers in a lot of place and they work just fine and they do not peg the processor.

Bill
+2  A: 

The piece of functionality you're looking for is a RepeatButton, this will fire it's Click event repeatedly while the mouse button is held down. You can configure the delay and the interval of the events.

You could then style this button to use the image at Silverlight Show

Hope this helps.

Nigel Sampson