How do we separate click and double click on listview in WPF application?

前端 未结 3 526
自闭症患者
自闭症患者 2021-01-25 05:25

I have a WPF application. There is a listview in which a every time I click or double click, the click event fires up. Even if I keep the Click Event, it automatically fires up

3条回答
  •  情话喂你
    2021-01-25 05:57

    The second click of a double-click is by definition always preceded by a single click.

    If you don't want to handle it you could use a timer to wait for like 200 ms to see if there is another click before you actually handle the event:

    public partial class MainWindow : Window
    {
        System.Windows.Threading.DispatcherTimer _timer = new System.Windows.Threading.DispatcherTimer();
        public MainWindow()
        {
            InitializeComponent();
            _timer.Interval = TimeSpan.FromSeconds(0.2); //wait for the other click for 200ms
            _timer.Tick += _timer_Tick;
        }
    
        private void lv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if(e.ClickCount == 2)
            {
                _timer.Stop();
                System.Diagnostics.Debug.WriteLine("double click"); //handle the double click event here...
            }
            else
            {
                _timer.Start();
            }
        }
    
        private void _timer_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("click"); //handle the Click event here...
            _timer.Stop();
        }
    }
    

    
    

提交回复
热议问题