Reputation: 1484
I have a FrameworkElement and I want to perform action A when the user single clicks, and action B when the user double clicks.
Due to the way events are delivered, I always get a single click event which begins action A. After looking around, I found an interesting technique here using a timer to delay the handling of the clicks. However, this example hardcodes the timer to 300 milliseconds, but I would prefer to use the user's "Double-click speed" setting Control Panel's Mouse Properties dialog.
What's the wpf/C# API for getting that value from the system?
Upvotes: 6
Views: 9408
Reputation: 365
I have a FrameworkElement and I want to perform action A when the user single clicks, and action B when the user double clicks.
If you're handling an event that provides a MouseButtonEventArgs
, such as UIElement.MouseLeftButtonDown
, you can access MouseButtonEventArgs.ClickCount
to determine if multiple clicks have happened within the double-click time of each other. Since FrameworkElement
derives from UIElement
this should be possible in your case.
No need to use DllImport or WinForms, as you don't really need to know the double-click time to accomplish your main goal.
Edit add: I discovered that this only really works for the ButtonDown events (left or right). I'm not sure why they didn't implement this properly for the ButtonUp events, but it should suffice to catch both events, and keep a member variable of the ClickCount from the ButtonDown that you can check when receiving ButtonUp.
Upvotes: 0
Reputation: 1413
If you don't want reference System.Windows.Forms assembly, you can try this:
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int GetDoubleClickTime();
Upvotes: 7
Reputation: 70170
You can find the time here: System.Windows.Forms.SystemInformation.DoubleClickTime
You can actually see a full implementation of what you are trying to achieve here:
WPF: Button single click + double click issue
Upvotes: 10