Ignore Single Click in WPF

2019-07-29 11:57发布

问题:

I have a user control wherein I would like to do something different in the case of a single click vs. double click. I'm handling mouseLeftButtonDown event as such:

 private void cueCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        switch (e.ClickCount)
        {
            case 1:
                {
                    cueCanvas.Focus();
                    e.Handled = true;
                    _mouseStartPoint = Mouse.GetPosition(null);
                    break;
                }
            case 2:
                {
                    double pos = Mouse.GetPosition(cueCanvas).X;
                    TimeSpan time = ConvertFromPosition(pos);
                    AddNewEvent(time);
                    e.Handled = true;
                    break;
                }
        }
    }

The problem is that WPF doesn't really care how many times you've clicked the mouse so I get the first click event even though I'm about to get a second one (in the case of a double-click). Has anyone come up with a way to work around this? I realize I could try to get clever and set some timers such that the first click gets "canceled" in the event that the second one comes in (I realize this is what the framework would be doing anyway). If that's the only answer, does anyone know how we query the system for the appropriate click delay?

回答1:

To my knowledge, there's really no way to do this. The problem is that you're fighting reality. :-) In reality, there are 2 clicks to a double-click. You want single click to be ignored if quickly followed by a double-click.

You'll have wait a short interval to see if it's followed by a second click. You can query for that interval using the SystemInformation class from WinForms, or just call the Win32 API directly.