Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Mouse Double Click Helper in Silverlight 4.0

0.00/5 (No votes)
18 Jul 2012 1  
Custom mouse double click helper class for Silverlight.

Introduction

The code snippet will decide if the user has double clickled on a control or not in Silverlight (or you can use it in anywhere but I will demonstrate it only for Silverlight).

Background

As double click support is not there in Silverlight we have decided to write our own code for the same.

Using the code

TestListBox.MouseLeftButtonDown += new MouseButtonEventHandler(TestListBox_MouseLeftButtonDown);
TestListBox.MouseLeftButtonUp += new MouseButtonEventHandler(TestListBox_MouseLeftButtonUp);

Use the below code for event handling.

/// <summary>
/// Handles the Mouse left button down event
/// </summary>
/// <param name="sender"></param>
/// <param name="arguments"></param>
private void TestListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs arguments)
{
  try
  {
    arguments.Handled = true;
  }
  catch (Exception exception)
  {
  }
}
/// <summary>
/// Handles Mouse left button up event
/// </summary>
/// <param name="sender"></param>
/// <param name="arguments"></param>
private void TestListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs arguments)
{
    bool doubleClick;
    try
    {
        doubleClick = MouseDoubleClickHelper.IsDoubleClick(sender, arguments);
        if (doubleClick)
        {      // - Do some work. 
        }
    }
    catch (Exception exception)
    {
    }
}

See attached code for the same. Based on the value of doubleClick variable user can perform any task.

Points of Interest

While writing/analyzing the code I saw that WPF already has a doubleClick event but Silverlight does not. Hope it will be there in the next version. Oh it's there in Silverlight 5 see http://www.silverlight.net/learn/overview/what's-new-in-silverlight-5/silverlight-5-mouse-button-double-and-multi-click.

But we are using Silverlight 4.0 that's really bad. However I think this will be really helpful for those who still use oldies technologies like me.

History

Initial version.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here