的主要思想是調用的DoDragDrop在PreviewMouseMove()事件,馬託當移動偏移大於不知何故值大。
1)這裏列表框中:
<ListBox AllowDrop="True" Drop=" ListBox_Drop" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" PreviewMouseMove="ListBox_PreviewMouseMove">
ListBoxItems是擴展器,如果我們實現DragAndDrop不能擴大。
2)現在,我們必須添加2個變量(我用VB.NET):
Private isDragging As Boolean = False 'flag: is drag operation in process?'
Private dragStartPoint As Point 'coords of dragging start.'
3)記得預覽點擊鼠標起點座標:
Private Sub ListBox_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs)
dragStartPoint = e.GetPosition(Me)
End Sub
4)在PreviewMouseMove得到將起點移動到當前的移動點偏移量。如果偏移大於某個值,我們啓動DragAndDrop操作並設置標誌isDragging來記住這一點。
Private Sub ListBox_PreviewMouseMove(sender As System.Object, e As MouseEventArgs)
If e.LeftButton = MouseButtonState.Pressed Then
Dim diff As Vector = Point.Subtract(dragStartPoint, e.GetPosition(Me))
If (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance) OrElse (Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) Then
If Not isDragging Then
isDragging = True 'the flag is active until drop event raises.'
Dim lstBox As ListBox = TryCast(sender, ListBox) 'get sender ListBox'
If lstBox IsNot Nothing Then
Dim data As Object = GetDataFromListBox(lstBox, e.GetPosition(lstBox)) 'get data for drag-and-drop; need to be realized; there are some realizations at Stackoverflow.com presented.'
Dim effects As DragDropEffects = DragDrop.DoDragDrop(lstBox, data, DragDropEffects.Move) 'initiate drag-and-drop.'
End If
End If
End If
End If
End Sub
5)等待處理拖放操作:
Private Sub ListBox_Drop(sender As Object, e As DragEventArgs)
isDragging = False 'reset isDragging flag.'
Dim lstBox As ListBox = TryCast(sender, ListBox) 'get sender ListBox.'
If lstBox IsNot Nothing Then
Dim myObj As MyClass = TryCast(e.Data.GetData(GetType(MyClass)), MyClass)
'...some actions'
End If
End Sub
我已經意識到這個想法,它的工作原理完全我的需求:
- 上MouseLeftButtonClick ListBoxItems與擴展器膨脹和 崩潰,
- 在MouseMove上按下左鍵DragAndDrop操作是 作品, ListBoxItems能夠被排序。
你可以發佈你的XAML模式嗎?我想,這還不足以思考解決方案。 –