A while back I needed to add a double click event to a control that had no such event. The solution I came up with was to wrap that control inside a UserControl and use the UserControl’s DoubleClick event.

One of the readers commented about a better way to do it using Command Binding, here is what David N said:

You can also do this by adding an InputBinding to the Grid, that will translate a double click to an invocation of a RoutedCommand, then you can bind that to some handler code somewhere higher in the tree. Commands seem to be a neglected part of WPF, but are really quite useful especially if there are multiple actions that should end up triggering the same code (button, toolbar button, context menu, key combination, double click in the grid..). I just used a builtin command in this sample but of course you’d probably use a custom one.

I was faced with the same problem last week so I decided to give it a shot.

Creating the Command

All you have to do is to define a RoutedUICommand object in the code behind of the control:

1:publicreadonlystatic RoutedUICommand RectangleDoubleClickedCommand;

Here is how to initialize the command I used a static constructor for it:

1:Ability to bind multiple event to multiple commands.
2:In CommandArgs in command handler you can even get original event args this was not possible with existing command binders.
3:In CommandArgs in command handler you can even get original source of the event this was not possible with existing command binders.
4:In this you can control individual CanExecute methods.