I would like to control if the user do single click or double click when is clicked with the mouse. So I use this code:
private void MouseSingleClickCommand(RoutedEventArgs e)
{
_dtMouseClick.Start();
}
private void MouseClick_Tick(object sender, System.EventArgs e)
{
_dtMouseClick.Stop();
//my one click code
}
private void MouseDoubleClickCommand(MouseButtonEventArgs e)
{
_dtMouseClick.Stop();
//code of the double click
}
_dt is a DispatcherTimer that is created in the constructor of the view model:
_dtMouseClick =
new System.Windows.Threading.DispatcherTimer(
new TimeSpan(0, 0, 0, 0, 200),
System.Windows.Threading.DispatcherPriority.Background,
MouseClick_Tick,
System.Windows.Threading.Dispatcher.CurrentDispatcher);
However the code of one click is always executed because the dispatcher is not stopped.
Why?.
Thanks.
EDIT: I include the axml of the button:
<StackPanel Height="Auto" HorizontalAlignment="Left" Margin="548,0,0,0" Name="stpBotnoesBasicos" VerticalAlignment="Top" Width="Auto">
<Button Content="Buscar" Height="23" Name="btn01" Width="75">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cmd:EventToCommand Command="{Binding MouseSingleClickCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseDoubleClick">
<cmd:EventToCommand Command="{Binding MouseDoubleClickCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Stackpannel>
This solution is based on the solution from this other question:
base solution
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.
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.
The dispatcherTimer is create in the constructor of the view model
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.