Ho lavorato un po sul drag and drop sempre per il mio lavoretto sul tris.
Lascio qui il codice, magari potesse servire a qualcuno
Class Window1
Private startPoint As Point = Nothing
Private selectedElementOrigins As Point = Nothing
Private isDragging As Boolean
Private selectedElement As UIElement
Private Sub Window1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
End Sub
Protected Overrides Sub OnPreviewMouseLeftButtonDown(ByVal e As System.Windows.Input.MouseButtonEventArgs)
MyBase.OnPreviewMouseLeftButtonDown(e)
'Se l'oggetto che ha scatenato l'evento non è la "window" allora vai avanti
If (Not (e.Source Is Me)) Then
'Se la variabile è false
If Not (isDragging) Then
'Dammi le coordinate di partenza del click del mouse
startPoint = e.GetPosition(Me)
'Identifica la sorgente che ha scatenato gli eventi
selectedElement = e.Source
Me.CaptureMouse()
isDragging = True
selectedElementOrigins = New Point(Canvas.GetLeft(selectedElement), Canvas.GetTop(selectedElement))
End If
e.Handled = True 'impostando a true si arresta il routing dell'evento
End If
End Sub
Protected Overrides Sub OnPreviewMouseLeftButtonUp(ByVal e As System.Windows.Input.MouseButtonEventArgs)
MyBase.OnPreviewMouseLeftButtonUp(e)
If (Me.IsMouseCaptured) Then
isDragging = False
Me.ReleaseMouseCapture()
e.Handled = True
End If
End Sub
Protected Overrides Sub OnPreviewMouseMove(ByVal e As System.Windows.Input.MouseEventArgs)
MyBase.OnPreviewMouseMove(e)
If (Me.IsMouseCaptured) Then
If (isDragging) Then
Dim CurrentPosition As New Point(e.GetPosition(Me).X, e.GetPosition(Me).Y)
Dim ElementLeft As Double = (CurrentPosition.X - startPoint.X) + selectedElementOrigins.X
Dim ElementTop As Double = (CurrentPosition.Y - startPoint.Y) + selectedElementOrigins.Y
Canvas.SetLeft(selectedElement, ElementLeft)
Canvas.SetTop(selectedElement, ElementTop)
End If
End If
End Sub
End Class
Funziona con un canvas, e un qualsiasi elemento UiElement