I am simulating a handheld device with WPF and MVVM (viewmodel is a state-machine, view is a simulated plastic case with buttons).
The gesture that turns the device on and off is a "long click" in a button. For exemple, during use, if I press "OK" button, it displays some screen, but if I HOLD it clicked for more than three seconds, it should (in a simulated way) turn the device off.
I took a look in RepeatButton, with its Delay and Interval properties, but those seem to fire the same Click event. What I need is to fire a regular Click if I hold the button for less than three seconds, and fire another, different LongClick (possibly once) if I hold it for more than three seconds.
How can I do this, using RepeatButton or even a regular Button?
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.
And here's a solution for RepeatButton:
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.