WPF实现双击事件MouseDoubleClick

本实例是基于Canvas增加双击事件

public class RevitCanvas : Canvas
    {
        public RevitCanvas()
        {
            _oncetime = long.MaxValue;
            Focusable = true;
            this.AddHandler(Canvas.PreviewMouseDownEvent, new MouseButtonEventHandler(OnMouseDown));

           //这里是使用使用双击事件哦
            this.AddHandler(RevitCanvas.MouseDoubleClick, new RoutedEventHandler(OnMouseDoubleClick));
        }

        public static RoutedEvent MouseDoubleClick = EventManager.RegisterRoutedEvent("MouseDoubleClick", RoutingStrategy.Bubble, typeof(MouseButtonEventHandler), typeof(RevitCanvas));

        protected void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            base.OnMouseDown(e);

            //双击,两次单机距离不超过4像素,时间再0.5秒以内视为双击
            Point p = e.GetPosition(this);
            long time = DateTime.Now.Ticks;
            if (Math.Abs(p.X - _oncePoint.X) < 4 && Math.Abs(p.Y - _oncePoint.Y) < 4 && (time - _oncetime < 5000000))
            {
                this.RaiseEvent(new RoutedEventArgs(RevitCanvas.MouseDoubleClick));//此处促发双击事件
            }
            _oncetime = time;
            _oncePoint = p;
        }

        private void OnMouseDoubleClick(object sender, RoutedEventArgs e)
        {
           //我是双击事件哦
        }

        private Point _startPoint;
        private long _oncetime;
        private Point _oncePoint;
    }    

双击事件使用:

        this.AddHandler(RevitCanvas.MouseDoubleClick, new RoutedEventHandler(OnMouseDoubleClick));

        private void OnMouseDoubleClick(object sender, RoutedEventArgs e)
        {
           //我是双击事件哦
        }
原文地址:https://www.cnblogs.com/mqxs/p/9645015.html