Silverlight: attach a double click to any object

Due to Silverlight lack of double click event a custom implementation is needed.
this function and its adjacent handlers will attach a double click event to any object deriving from UIElement.

public class GraphicsUtils
{
	#region Double click

	/// <summary>
	/// Holds the millis for the first click.
	/// </summary>
	private long lastClickedTime = 0;

	/// <summary>
	/// Delegate definition for the call back function.
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	public delegate void DoubleClickHandler(object sender, MouseButtonEventArgs e);

	/// <summary>
	/// Holder for the call back function.
	/// </summary>
	private DoubleClickHandler doubleClickCallBackFunction;
	/// <summary>
	/// This will attach a double click to an object.
	/// </summary>
	/// <remarks>
	/// Due to Silverlight lack of double click event a custom implementation is needed.
	/// this function and its adjacent handlers will attach a double click event to any object
	/// deriving from UIElement.
	/// </remarks>
	/// <param name="doubleClickSender">the UIElement to attach the event to.</param>
	/// <param name="function">the function to call upon the double click event.</param>
	public void AttachDoubleClick(object doubleClickTarget, DoubleClickHandler function)
	{
		UIElement target = (UIElement)doubleClickTarget;
		target.MouseLeftButtonUp += new MouseButtonEventHandler(target_MouseLeftButtonUp);
		target.MouseLeftButtonDown += new MouseButtonEventHandler(target_MouseLeftButtonDown);
		doubleClickCallBackFunction = function;
	}

	private void target_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
	{
		lastClickedTime = DateTime.Now.Ticks / 10000;
	}

	private void target_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
	{
		long currentMillis = DateTime.Now.Ticks / 10000;

		if (currentMillis - lastClickedTime < 100 && currentMillis - lastClickedTime > 0)
		{
			//HERE IS DOUBLE CLICK
			doubleClickCallBackFunction(sender, e);
		}

	}

	#endregion
}

then... this is how you use it:

GraphicsUtils.DoubleClickHandler dblClickHandler = new GraphicsUtils.DoubleClickHandler(MyMethodToHandleDoubleClick);
GraphicsUtils gu = new GraphicsUtils();
gu.AttachDoubleClick(MyObjectDerivingFromUIElement, dblClickHandler);
Advertisements

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: