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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!