一个rectangle控件的事件
<Rectangle Name="btnDoubleClick" Width="50" Fill="Red" MouseLeftButtonDown="btnDoubleClick_MouseLeftButtonDown" MouseLeftButtonUp="btnDoubleClick_MouseLeftButtonUp" />
#region Custom Double Click DateTime clickTime; bool isClicked = false; private void btnDoubleClick_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { System.Diagnostics.Debug.WriteLine("Down"); if (isClicked) {
isClicked = false; TimeSpan span = DateTime.Now - clickTime; if (span.Milliseconds < System.Windows.Forms.SystemInformation.DoubleClickTime) { System.Diagnostics.Debug.WriteLine("Double Click"); } } else { isClicked = true; clickTime = DateTime.Now; } } private void btnDoubleClick_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { System.Diagnostics.Debug.WriteLine("Up"); } #endregion
我的问题是,如果是双击怎么不输出“Down”和“Up”?单击的时候只输出“Down”和“Up”?
首次按下弹起的时候就已经输出了。要么就把单击的输出做个延时,这期间如果发生双击事件则不输出单击。
思路:设置一个双击标识,单击事件延时处理。如弹起事件
new Thread( () => { Thread.Sleep(DoubleClickTime); if (!isDoubleClick) { this.Dispatcher.Invoke( (Action)(() => { System.Diagnostics.Debug.WriteLine("Up"); }) ); } } ).Start();