当前位置:网站首页>Solution to control double click conflict in WPF

Solution to control double click conflict in WPF

2022-06-10 05:02:00 Shunnet

When you are setting a button to click and double-click [ Normally there are two events ]

After the event is created , Clicking the control is normal , Go to click event

When double-click , You'll find that , It will first click on the event , Then enter the double click event , It's a headache

【 On the talent , Flower hands shake up 】

Shake tired , Take a look at the code 【 An event is settled 】

/// <summary>
///  Number of hits 
/// </summary>
int CkickCount = 0;
/// <summary>
///  Double click 
/// </summary>
bool IsDoubleClick = false;
/// <summary>
///  When the mouse presses 
/// </summary>
private void Button_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ++ChannelCkickCount;
    DispatcherTimer ClickTimer = new DispatcherTimer();
    ClickTimer.Interval = new TimeSpan(0, 0, 0, 0, 200);
    ClickTimer.Tick += (s, e1) =>
    {
        ClickTimer.IsEnabled = false;
        ChannelCkickCount = 0;
        if (!ChannelIsDoubleClick) { ClickCommand(); }
        ChannelIsDoubleClick = false;
    };
    ClickTimer.IsEnabled = true;
    if (ChannelCkickCount % 2 == 0)
    {
        ClickTimer.IsEnabled = false;
        ChannelCkickCount = 0;
        ChannelIsDoubleClick = true;
        DoubleClickCommand();
    }
}

 /// <summary>
///  single click 
/// </summary>
private void ClickCommand()
{

}

/// <summary>
///  double-click 
/// </summary>
private void DoubleClickCommand()
{

}

Copy

Realization

// Normal operation 
 The object of the button .PreviewMouseLeftButtonDown +=Button_PreviewMouseLeftButtonDown;
// Operation with parameters 【 The event method also has to define parameters 】
 The object of the button .PreviewMouseLeftButtonDown += delegate (object sender, MouseButtonEventArgs e) {Button_PreviewMouseLeftButtonDown(sender, e, 【 Here are the passed parameters 】); };

Copy

Get it done , Point like collection and pay attention to

“ Focus on [ Downstream network ] WeChat official account , Learn more and more interesting real-time information ”

The author of this article :[ Blogger ] dashun

Link to this article :https://shunnet.top/BJ36bi

Copyright notice : Reprint with reference to , thank you

原网站

版权声明
本文为[Shunnet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206091203002980.html