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

后端 未结 9 1390
鱼传尺愫
鱼传尺愫 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:42

    You are simply can use MouseDown event and count click number, like this:

    if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
    {
        // your code here
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-15 06:49

    It's my working solution :)

        #region message label click --------------------------------------------------------------------------
    private Timer messageLabelClickTimer = null;
    
    private void messageLabel_MouseUp(object sender, MouseButtonEventArgs e)
    {
      Debug.Print(e.ChangedButton.ToString() + " / Left:" + e.LeftButton.ToString() + "  Right:" + e.RightButton.ToString() + "  click: " + e.ClickCount.ToString());
      // in MouseUp (e.ClickCount == 2) don't work!!  Always 1 comes.
      // in MouseDown is set e.ClickCount succesfully  (but I don't know should I fire one clicked event or wait second click)
    
      if (e.ChangedButton == MouseButton.Left)
      {
        if (messageLabelClickTimer == null)
        {
          messageLabelClickTimer = new Timer();
          messageLabelClickTimer.Interval = 300;
          messageLabelClickTimer.Elapsed += new ElapsedEventHandler(messageLabelClickTimer_Tick);
        }
    
        if (! messageLabelClickTimer.Enabled)                                                 
        { // Equal: (e.ClickCount == 1)
          messageLabelClickTimer.Start();
        }
        else  
        { // Equal: (e.ClickCount == 2)
          messageLabelClickTimer.Stop();
    
          var player = new SoundPlayer(ExtraResource.bip_3short);               // Double clicked signal
          player.Play();
        }
      }
    }
    
    private void messageLabelClickTimer_Tick(object sender, EventArgs e)
    { // single-clicked
      messageLabelClickTimer.Stop();
    
      var player = new SoundPlayer(ExtraResource.bip_1short);                  // Single clicked signal
      player.Play();
    }    
    #endregion
    
    0 讨论(0)
提交回复
热议问题