Nuts
Nuts

Reputation: 2813

Drag item from ListView

How to initiate drag-and-drop operation from WPF ListView?

I do not find how to catch the to-be-dragged ListViewItem from the event handler. I tried to subscribe the above Tunneling event:

Private Sub MyListView_PreviewMouseLeftButtonDown(sender As System.Object, e As System.Windows.Input.MouseButtonEventArgs)
    Dim lstview As ListView = CType(sender, ListView)
    If lstview.SelectedItem IsNot Nothing Then
        DragDrop.DoDragDrop(lstview, lstview.SelectedItem, DragDropEffects.Move)
    End If
End Sub 

...but as expected, the ListViewItem is not yet set when this event is handled here and is Nothing always.

Handling ListView's MouseDown won't work either since that doesn't fire when ListViewItem is clicked.

How to get hold on the ListView item that is to be dragged? It should support dragging multiple ListViewItems as well.

Upvotes: 0

Views: 152

Answers (1)

Sheridan
Sheridan

Reputation: 69987

You can start your drag operation from the SelectionChanged event handler:

Private Sub ListBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)

    Dim selectedItems As List(Of YourDataType) = 
        e.AddedItems.OfType(Of YourDataType)().ToList()
    DragDrop.DoDragDrop( ... )

End Sub

Upvotes: 1

Related Questions