What is the best way to simulate a Click with MouseUp & MouseDown events or otherwise?

前端 未结 5 720
天涯浪人
天涯浪人 2020-11-30 02:17

In WPF most controls have MouseUp and MouseDown events (and the mouse-button-specific variations) but not a simple Click event that ca

5条回答
  •  再見小時候
    2020-11-30 02:28

    Here is a simple solution that I use on a fairly regular basis. It is easy, and works in all sorts of scenarios.

    In your code, place this.

    private object DownOn = null;
    
    private void LeftButtonUp(object sender, MouseButtonEventArgs e)
    {
      if (DownOn == sender)
      {
        MessageBox.Show((sender as FrameworkElement).Name);
      }
      DownOn = null;
    }
    
    private void LeftButtonDown(object sender, MouseButtonEventArgs e)
    {
      DownOn = sender;
    }
    

    Now all you have to do is setup handlers for all of the controls that you care about i.e.

    
    
    

    If you have a ton of controls, and don't want to do it all in XAML, you can do it in your controls / window constructor programatically.

    This will cover most of your scenarios, and it can easily be tweaked to handle special cases.

提交回复
热议问题