Home > MVVM, Programming, Silverlight > Single/Double click Silverlight 4 trigger

Single/Double click Silverlight 4 trigger


This one’s about triggers…

Having as start point Shemesh‘s blog post http://shemesh.wordpress.com/2010/07/06/silverlight-double-click-trigger-call-method-action/ I modded his code to my own needs.

First of all I wanted a trigger that can do single and double click at the same time. Second, I needed this to be in the spirit of the MVVM architecture.

So, to do this I added a counter in the mouse button up event so I can count the clicks the user makes.

I also added two dependency properties for the commands that the single click and double click actions will trigger.

The project is the most simple possible MVVM example using this SingleDoubleClickTrigger

Source code: here

Advertisements
  1. Shemesh
    25/11/2010 at 4:28 pm

    you have a bug there.
    on method OnDetaching() you un-register to MouseLeftButtonDown event.
    it should be MouseLeftButtonUp. Up!

    • 25/11/2010 at 5:21 pm

      Thanks for noticing that! I will update the code shortly 🙂

    • 25/11/2010 at 5:24 pm

      Code updated. Available at the same link

  2. Uma Nagarajan
    17/02/2011 at 9:07 pm

    Great posts! I started with the code from https://technologywanderer.wordpress.com/2010/09/19/singledouble-click-silverlight-4-trigger/ and extended it to meet my needs – so the trigger could invoke not only Commands, but to invoke any action (InvokeCommandAction, ControlStoryboardAction, ChangePropertyAction, etc).

    I added an enum which will be used to set the Click Type on the trigger.
    public enum ClickTypes
    {
    SingleClick,
    DoubleClick
    }

    Added a property ClickType to the Trigger and defaulted it to SingleClick:
    public ClickTypes ClickType
    {
    get;
    set;
    }

    Modified the OnMouseButtonUp and OnTimerTick as follows:
    private void OnMouseButtonUp(object sender, MouseButtonEventArgs e)
    {
    if (!timer.IsEnabled)
    {
    timer.Start();
    }
    clicks++;
    }

    private void OnTimerTick(object sender, EventArgs e)
    {
    timer.Stop();
    if (clicks > 1 && ClickType == ClickTypes.DoubleClick)
    {
    InvokeActions(null);
    }
    else if (clicks == 1 && ClickType == ClickTypes.SingleClick)
    {
    InvokeActions(null);
    }
    clicks = 0;
    }

    Now we can pick any Action from Blend and pick this trigger for TriggerType and set ClickType and we can Invoke Commands like the ones in this example or invoke other actions like ChangePropertyAction for example.

    • 17/02/2011 at 9:15 pm

      That is one great use for the trigger. Glad it helped you out 😉

  1. No trackbacks yet.

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: