My contemplations and diurnal novelties are publicized here

In WinForms, everything that derived from System.Windows.Forms.Control had the MouseDoubleClick event – and not suprisingly, in WPF, everything derived from System.Windows.Controls.Control also has a MouseDoubleClick event. But there is a key difference – in WinForms, almost everything that gets displayed on the screen is derived from System.Windows.Forms.Control in some way – while in WPF, there are a number of items you can put on the screen that do not actually derive from System.Windows.Controls.Control. So today we are going to take a look at how to get double clicks when you are not derived from System.Windows.Controls.Control.

To figure this out, we are going to write a little app with a TextBlock – and when you double click on the TextBlock, it switches to a TextBox where you can edit the text. When you are done editing the text, you can double click in the TextBox, and it switches back to the TextBlock with the new text that you have entered. You can see screenshots of the two states of this simple app below:

Ok, so the issue here is that TextBlock does not derive from Control (it derives from FrameworkElement), so it does not have a MouseDoubleClick event to attach to. So what do we do? Well, it is actually really simple – lets take a look at the xaml behind this sample app first:

       Text=“{Binding ElementName=MyWindow, Path=MyText}”/>



So as you can see, we just have a TextBlock and a TextBox, one visible and one not. On the TextBox we have attached to the MouseDoubleClick event, and on the TextBlock we have attached to the MouseDown event. So, as you might have suspected, we will be using the MouseDown event to figure out if a double click has occurred. So onto the code behind:

class DoubleClickWindow :
  public DoubleClickWindow()

string MyText
    set {
SetValue(MyTextProperty, value);

DependencyProperty MyTextProperty =
      DependencyProperty.Register(“MyText”, typeof(string),
        typeof(DoubleClickWindow), new

UIPropertyMetadata(“This Is Some Text Of Mine”));

void MyBlock_MouseDown(object sender, MouseButtonEventArgs e)


void MyBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)


So here we have the whole code behind class for DoubleClickWindow. Not much here, just a constructor, the dependency property that the TextBlock and TextBox are bound to, and the two event functions.

And it is one of those event functions that we care about – the MyBlock_MouseDown method. And what do we have here – a ClickCount number in the MouseButtonEventArgs! The ClickCount property holds the number of times the mouse button has been clicked within the system double click time (you know, that slider for double click speed in the mouse control panel). If you want to know if the user has clicked twice, you just have to check and see if the ClickCount is equal to 2 – and that is exactly what we do here. If it is equal to 2, we switch the visibilities on the TextBlock and the TextBox.

The interesting thing about the ClickCount is that you can do something like listen for a triple or even quadruple click. I have no idea why you would want to do such a thing 😛 – but it is possible. You can also get this information in any click or mouse up event (or any of the corresponding preview events) – any event that gives you the MouseButtonEventArgs as the argument to the event will have the click count information.

Oh, and I suppose we can’t forget the last function in the class – MouseDoubleClick, which is hooked to the actual double click event on the TextBox. Of course here we don’t have to check if it is a double click (the code wouldn’t be executing otherwise) – we just flip the visibilities back. And, just as a side note, if you dive down into the code of Control (where this double click event comes from), you find that all they are doing is triggering the event when the ClickCount of the current mouse down event is equal to 2.

Hope that cleared up any confusion that you may have had about double click events in WPF! I know I was confused for a little while as to why there wasn’t a MouseDoubleClick event available everywhere. As always, please leave and questions in the comments and I’ll do my best to answer them.

Copied from


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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: