silverlight双击事件处理
首先添加双击处理帮助类:
public static class DoubleClickHelper { private const long k_DoubleClickSpeed = 300; private const double k_MaxMoveDistance = 10; private static long m_LastClickTicks = 0; private static Point m_LastPosition; private static object m_LastSender; /// <summary> /// 鼠标双击事件处理。 /// </summary> /// <param name="sender">类型对象</param> /// <param name="e">事件数据</param> /// <returns>bool</returns> public static bool IsDoubleClick(object sender, MouseButtonEventArgs e) { bool senderMatch = sender.Equals(m_LastSender); m_LastSender = sender; long clickTicks = DateTime.Now.Ticks; Point position = e.GetPosition(null); if (senderMatch) { long elapsedTicks = clickTicks - m_LastClickTicks; long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond; double distance = position.Distance(m_LastPosition); if (elapsedTime <= k_DoubleClickSpeed && distance <= k_MaxMoveDistance) { // Double click! m_LastClickTicks = 0; return true; } } // Not a double click m_LastClickTicks = clickTicks; m_LastPosition = position; return false; } // 获取间距 private static double Distance(this Point pointA, Point pointB) { double x = pointA.X - pointB.X; double y = pointA.Y - pointB.Y; return Math.Sqrt(x * x + y * y); } }
页面代码:
<sdk:DataGrid MouseLeftButtonUp="dg_MouseLeftButtonUp" ></sdk:DataGrid>
后台代码这样写:
private void dg_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { if (dg.SelectedItem != null) { if (DoubleClickHelper.IsDoubleClick(sender, e)) { //双击事件处理。。。 } }