The simplest way to detect DoubleClick in Silverlight

by Jim McCurdy 7. August 2009 22:32

Here is a simple static class, MouseButtonHelper, that offers a single method, IsDoubleClick,  to determine if a standard MouseLeftButtonDown or MouseLeftButtonUp event is a double click.  In the past I have used timers, and Triggers and Behaviors to accomplish the same thing, but this approach is less code, less XAML, and uses a lot less resources.

Usage is as follows:

  • In XAML or in code behind, handle the standard MouseLeftButtonDown or MouseLeftButtonUp event for the object you wish to perform double click handling.
  • Implement the handler function for the above event as follows:
public void OnMouseButtonDown(object sender, MouseButtonEventArgs e)
{
	bool doubleClick = MouseButtonHelper.EventIsDoubleClick(sender, e);
	if (doubleClick)
		MessageBox.Show("Double click detected!", "Alert", MessageBoxButton.OK);
}

The code below has been tested and used with Silverlight 3 and Silverlight 4.

using System;
using System.Windows;
using System.Windows.Input;

namespace ClassLibrary
{
	internal static class MouseButtonHelper
	{
		private const long k_DoubleClickSpeed = 500;
		private const double k_MaxMoveDistance = 20;

		private static long m_LastClickTicks = 0;
		private static Point m_LastPosition;
		private static object m_LastSender;

		internal static bool IsDoubleClick(object sender, MouseButtonEventArgs e)
		{
			bool senderMatch = sender.Equals(m_LastSender);
			m_LastSender = sender;

			long clickTicks = DateTime.Now.Ticks;
			Point position = e.GetPosition(null);
			if (senderMatch)
			{
				long elapsedTicks = clickTicks - m_LastClickTicks;
				long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond;
				double distance = position.Distance(m_LastPosition);
				if (elapsedTime <= k_DoubleClickSpeed && distance <= k_MaxMoveDistance)
				{
					// Double click!
					m_LastClickTicks = 0;
					return true;
				}
			}

			// Not a double click
			m_LastClickTicks = clickTicks;
			m_LastPosition = position;
			return false;
		}

		private static double Distance(this Point pointA, Point pointB)
		{
			double x = pointA.X - pointB.X;
			double y = pointA.Y - pointB.Y;
			return Math.Sqrt(x * x + y * y);
		}
	}
}
Bookmark and Share

Comments

1/29/2010 3:42:10 PM #

trackback

The simplest way to detect DoubleClick in Silverlight

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com | Reply

2/1/2010 5:58:39 PM #

trackback

The simplest way to detect DoubleClick in Silverlight

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout | Reply

8/5/2010 1:34:44 PM #

Hector

Cool

This really helpme a LOT ! !

Hector | Reply

Add comment


(Will show your Gravatar icon)

Click to change captcha
biuquote
  • Comment
  • Preview
Loading




My Photo Jim is always looking for new clients in need of software development expertise.  He is particularly well suited for Silverlight, .NET, ASP.NET, and WPF projects.

Professional Biography

Jim McCurdy operates Face to Face Software, where he works designing, developing, and managing software projects for a variety of clients.

Jim currently specializes in development projects for Silverlight, .NET, ASP.NET, and WPF platforms. One such project is a complete web site using Silverlight, .NET, and C#; a unique financial and lifestyle planning web application at YinYangMoney.com.

Jim has worked in the software industry for as long as he can remember, and has broad expertise in Web, Windows, and systems technologies. Jim has been a team player on many agile development projects throughout his career, and is a founding member at software startups Astral Development and Powerhouse Entertainment.

You can Email Jim at Face to Face Software.


Welcome Readers