How can I catch both single-click and double-click events on WPF FrameworkElement?

后端 未结 9 1471
鱼传尺愫
鱼传尺愫 2020-12-15 06:21

I can catch a single-click on a TextBlock like this:

private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    MessageBo         


        
9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 06:48

    You need to fire the event after the click sequence is over... when is that? I suggest using a timer. The MouseDown event would reset it and increase the click count. When timer interval elapses it makes the call to evaluate the click count.

        private System.Timers.Timer ClickTimer;
        private int ClickCounter;
    
        public MyView()
        {
            ClickTimer = new Timer(300);
            ClickTimer.Elapsed += new ElapsedEventHandler(EvaluateClicks);
            InitializeComponent();
        }
    
        private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
        {
            ClickTimer.Stop();
            ClickCounter++;
            ClickTimer.Start();
        }
    
        private void EvaluateClicks(object source, ElapsedEventArgs e)
        {
            ClickTimer.Stop();
            // Evaluate ClickCounter here
            ClickCounter = 0;
        }
    

    Cheers!

提交回复
热议问题