WPF 非Control元素模拟鼠标双击MouseDoubleClick事件

private readonly DispatcherTimer _mouseLeftTimer = new DispatcherTimer();
        private readonly DispatcherTimer _mouseRightTimer = new DispatcherTimer();


public Class()
{
            _mouseLeftTimer.Interval = TimeSpan.FromMilliseconds(MOUSE_CLICK_DELAY);
            _mouseLeftTimer.Tick += _mouseLeftTimer_Tick;
            this.PreviewMouseLeftButtonDown += AscanControlSciChart_PreviewMouseLeftButtonDown;

            _mouseRightTimer.Interval = TimeSpan.FromMilliseconds(MOUSE_CLICK_DELAY);
            _mouseRightTimer.Tick += _mouseRightTimer_Tick;
            this.PreviewMouseRightButtonDown += AscanControlSciChart_PreviewMouseRightButtonDown;
}



        private void _mouseLeftTimer_Tick(object sender, EventArgs e)
        {
            _mouseLeftTimer.Stop();
            ClickCommand?.Execute(this);
            _log.Info($"单击:{this.Title}");
        }
        private void AscanControlSciChart_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                _mouseLeftTimer.Stop();
                DoubleClickCommand?.Execute(this);
                _log.Info($"双击:{this.Title}");
            }
            else
            {
                _mouseLeftTimer.Start();
            }
        }

        private void _mouseRightTimer_Tick(object sender, EventArgs e)
        {
            _mouseRightTimer.Stop();
            RightClickCommand?.Execute(this);
            _log.Info($"右键单击:{this.Title}");
        }
        private void AscanControlSciChart_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount == 2)
            {
                _mouseRightTimer.Stop();
                //右键双击
            }
            else
            {
                _mouseRightTimer.Start();
            }
        }




        #region 事件
        public ICommand ClickCommand
        {
            get { return (ICommand)GetValue(ClickCommandProperty); }
            set { SetValue(ClickCommandProperty, value); }
        }

        public static readonly DependencyProperty ClickCommandProperty =
            DependencyProperty.Register(nameof(ClickCommand), typeof(ICommand), typeof(AscanControlSciChart), new PropertyMetadata(default(ICommand)));

        public ICommand DoubleClickCommand
        {
            get { return (ICommand)GetValue(DoubleClickCommandProperty); }
            set { SetValue(DoubleClickCommandProperty, value); }
        }

        public static readonly DependencyProperty DoubleClickCommandProperty =
            DependencyProperty.Register(nameof(DoubleClickCommand), typeof(ICommand), typeof(AscanControlSciChart), new PropertyMetadata(default(ICommand)));

        public ICommand RightClickCommand
        {
            get { return (ICommand)GetValue(RightClickCommandProperty); }
            set { SetValue(RightClickCommandProperty, value); }
        }

        public static readonly DependencyProperty RightClickCommandProperty =
            DependencyProperty.Register(nameof(RightClickCommand), typeof(ICommand), typeof(AscanControlSciChart), new PropertyMetadata(default(ICommand)));

        #endregion

 

posted on 2024-02-26 13:26  lopengye  阅读(195)  评论(0)    收藏  举报

导航