我有一个网格中有矩形的控件。我可以在网格中将矩形从一个单元格拖动到另一个单元格。但我还需要双击矩形以显示编辑对话框。这里的问题是,只需在矩形上按下鼠标左键就会发起MouseMove
事件,并导致DragDrop
在不需要时启动。
以下是MouseMove
和MouseDoubleClick
事件处理程序:
Dim IsDragging As Boolean = False
Private Sub SchedItem_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
If IsDragging Then Exit Sub
If e.LeftButton = MouseButtonState.Pressed Then
Dispatcher.InvokeAsync(Sub()
IsDragging = True
DragDrop.DoDragDrop(Me, Me, DragDropEffects.Move)
IsDragging = False
End Sub)
End If
End Sub
Private Sub SchedItem_PreviewMouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseDoubleClick
itmEditTask_Click(Nothing, Nothing)
e.Handled = True
End Sub
我尝试确定自MouseMove
事件以来鼠标移动的距离,因此尝试不启动MouseLeftButtonDown
。但x& y数字到处都是,我无法可靠地设置阈值。这是代码:
Private mPos As Point
Private Sub SchedItem_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
mPos = Mouse.GetPosition(Nothing)
End Sub
Private Function IsReallyMouseMove() As Boolean
Dim pPos As Point = Mouse.GetPosition(Nothing)
Debug.WriteLine(Math.Abs(mPos.X - pPos.X) & ", " & Math.Abs(mPos.Y - pPos.Y))
IsReallyMouseMove = Math.Abs(mPos.X - pPos.X) > 10 OrElse Math.Abs(mPos.Y - pPos.Y) > 10
End Function
所以我希望有人这样做并且可以提供一些方向。
答案 0 :(得分:0)
最佳方式 - 重新设计。 使项目元素中的某个对象可拖动项目。 (也许用不同的拖动光标小点) 其他区域将处理双击。