One way you could do this would be to bind to the MouseDown and MouseUp events. Use something like a Stopwatch that gets started on MouseDown and check the amount of time that's elapsed on MouseUp. If it's less than 3 seconds, do your Click() action. If it's more than 3 seconds, do your LongClick() action.
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
stopwatch = new Stopwatch();
stopwatch.Start();
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
stopwatch.Stop();
if (stopwatch.ElapsedMilliseconds >= 3000)
{
// do Click()
}
else
{
// do LongClick
}
}
And here's a solution for RepeatButton:
private bool isLongClick;
private bool hasAlreadyLongClicked;
private void RepeatButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
isLongClick = false;
hasAlreadyLongClicked = false;
stopwatch = new Stopwatch();
stopwatch.Start();
}
private void RepeatButton_Click(object sender, RoutedEventArgs e)
{
if (!hasAlreadyLongClicked && stopwatch.ElapsedMilliseconds >= 3000)
{
hasAlreadyLongClicked = true;
isLongClick = true;
// do your LongClick action
}
}
private void RepeatButton_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
if (!isLongClick)
{
// do short click action
}
}
The trick here is that a RepeatButton is basically just a Button that fires Click at every interval. So, if we start the Stopwatch on PreviewMouseDown for the button, we can check the elapsed time on the stopwatch every time the Click event fires, and modify our action based on the result.