Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Silverlight 5: Supporting Double and Even Triple Click for the Mouse

Pete Brown - 13 April 2011

Double-click isn't as popular a mouse action as it once was. For many, especially the elderly, clicking twice in the same spot without moving the mouse in between, is actually quite challenging. Nevertheless, double-click (and even triple click in some specialized programs in 3d and music) is a necessary capability.

Please note that this article and the attached sample code was written using the Silverlight 5 Beta, available at MIX11 in April 2011, and updated for the Silverlight 5 RC.

Silverlight 5 has introduced the concept of a click count. Rather than create dedicated double, triple, or septuple click events, you can simply count the number of clicks using the ClickCount property of the MouseButtonEventArgs class. This works for both the left and right mouse buttons.

Implementing this yourself is non-trivial, as you have to take into account some fairly fiddly timings, ensuring that two consecutive single clicks aren't taken as a double-click unless the second click happens within a specific time period after the previous click. I'm happy to let the platform do that work for me.

Project Setup

I created a simple Silverlight web project. In the MainPage.xaml, I added a single red rectangle and wired up an event handler for MouseLeftButtonDown.

<UserControl x:Class="MouseClickCountDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White">
<Rectangle x:Name="ClickMe"
Width="80"
Height="80"
MouseLeftButtonDown="ClickMe_MouseLeftButtonDown"
Fill="Red" />
</Grid>
</UserControl>

The only code-behind is simply an event handler that helps us count the clicks.

Implementing Double Click

Inside the ClickMe_MouseLeftButtonDown event, I added some debug code to write the number of clicks to the output window, and display a "Double Click" message if there are two clicks in a row.

private void ClickMe_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
System.Diagnostics.Debug.WriteLine("Click Count: " + e.ClickCount);

if (e.ClickCount == 2)
System.Diagnostics.Debug.WriteLine("Double Click");

}

That's all there is to it. I've been able to generate click counts up into the hundreds before my finger started to protest. There is no artificial limit on the number of clicks you can respond to. Try clicking slowly and clicking quickly to get a feel for how it responds.

Source code for the Silverlight 5 Beta version of this application may be downloaded via the link below.

A video version of this tutorial will be on Silverlight.net shortly.

       

Source Code and Related Media

Download /media/76804/mouseclickcountdemo.zip
posted by Pete Brown on Wednesday, April 13, 2011
filed under:        

7 comments for “Silverlight 5: Supporting Double and Even Triple Click for the Mouse”

  1. Scottsays:
    Appreciate the article, but an example like double-clicking an item in a listbox is more practical and real-world. But anyway...I used this code for a listbox, but then found the MouseLeftButtonDown event is handled internally by the listboxitem and it doesn't bubble back up to the code-behind. The solution to that problem is here: http://stackoverflow.com/questions/2809402/silverlight-listbox-firing-mouserightbuttondown-but-not-mouseleftbuttondown
  2. Mycroftsays:
    This event is useless when working with a grid, the item does not support the events. What a dramatic dissapointment I was hoping to get rid of all the hacks used to service a doubleclick on a datagrid row.

Comment on this Post

Remember me