This solution is based on the solution from this other question:
If I use the PreviewMouseLeftButtonDown to control when is single click or double click works, instead of using two events (click and doubleclick).
So I solve the problem with this code:
This first method control the click with the mouse.
private void MouseLeftButtonDownCommand(MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
_dtMouseClick.Start();
}
else if(e.ClickCount > 1)
{
_dtMouseClick.Stop();
//the code of the double click
}
}
This method is the method that is linked to the DispatcherTimer, that is execute if is not stopped with the second click of the mouse.
private void MouseClick_Tick(object sender, System.EventArgs e)
{
_dtrMouseClick.Stop();
//code of the single click
}
The dispatcherTimer is create in the constructor of the view model
_dtBotonBuscarMouseClick =
new System.Windows.Threading.DispatcherTimer(
new TimeSpan(0, 0, 0, 0, 250),
System.Windows.Threading.DispatcherPriority.Background,
MouseClick_Tick,
System.Windows.Threading.Dispatcher.CurrentDispatcher);
_dtMouseClick.Stop();
The interval is 250ms that is the interval that is the time that the user has to double click.
In this solution, I use the same way to stop the dispatcherTimer, the stop() method, but for some reason if I use the two events (click and mouseDoubleClick) the dispatcherTimer is not stopped in the double click and if I use the MouseLeftButtonDown event the solution works.
EDIT
This solution for me works as I expect.
In the MouseLeftButtonDown event, I check if the ClickCount is 1 or 2. If the count is 1, then I start the timer. So the timer will execute its code if is not stopped before.
The timer will be stopped if the second click is a double click. This is when the ClickCounter > 1. If the lapse time between clicks is enough big, then clickcounter is always 1, so this event, MouseLeftButtonDown, control when is double click or not.
When the timer is executed, first I stop it because I want exectued its code once, the code that I want when is only one click.
If I double click, then I stop the timer before the interval is reached, so never is executed, and execute the code that I want when is double click.