Silverlight: double click Trigger & call method Action

There are many different ways to implement double click in Silverlight, I chose to write it as a Trigger so it can be used anywhere and easily.

Of course you can write your own Action to use with that Trigger, but I wanted a most generic Action that will call a method and also could be used anywhere and easily.

The following Trigger (DoubleClick) & Action (InvokeMethodAction) classes act like any other event & handler. They can be used on any UIElement.

DoubleClick:

public class DoubleClick : TriggerBase<UIElement>
{
    private readonly DispatcherTimer _timer;
    private Point _clickPosition;

    public DoubleClick()
    {
        _timer = new DispatcherTimer
        {
            Interval = new TimeSpan(0, 0, 0, 0, 300)
        };

        _timer.Tick += OnTimerTick;
    }

    protected override void OnAttached()
    {
        base.OnAttached();

        AssociatedObject.MouseLeftButtonUp += new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);
    }

    void AssociatedObject_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        UIElement element = sender as UIElement;

        if (_timer.IsEnabled)
        {
            _timer.Stop();
            Point position = e.GetPosition(element);

            if (Math.Abs(_clickPosition.X - position.X) < 1 && Math.Abs(_clickPosition.Y - position.Y) < 1)
            {
                InvokeActions(null);
            }
        }
        else
        {
            _timer.Start();
            _clickPosition = e.GetPosition(element);
        }
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();

        AssociatedObject.MouseLeftButtonUp -= new MouseButtonEventHandler(AssociatedObject_MouseLeftButtonUp);
        if (_timer.IsEnabled)
            _timer.Stop();
    }

    private void OnTimerTick(object sender, EventArgs e)
    {
        _timer.Stop();
    }
}

InvokeMethodAction:

public class InvokeMethodAction : TargetedTriggerAction<UIElement>
{
    protected override void Invoke(object parameter)
    {
        if (MethodToInvoke != null)
        {
            MethodToInvoke(Target, null);
        }
    }

    public delegate void Handler(object sender, RoutedEventArgs e);
    public event Handler MethodToInvoke;

}

Download sample project here.

Advertisements

4 Responses to Silverlight: double click Trigger & call method Action

  1. Pingback: Single/Double click Silverlight 4 trigger « Tech Wanderer

  2. max says:

    Thanks for a great post!
    There is one thing which is not clear to me – why do we need two parameters for the Handler delegate. The second one is always null, isn’t it?

    • shemesh says:

      it is optional, you can remove it if you are not using it.

      • max says:

        It’s a funny thing – can’t make it compile once the Handler has anything else but two parameters. Compilation of MainPage.xaml fails for “No overload for ‘DoubleClickHandler’ matches delegate ‘Handler”. Any hints?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: