1
votes

I have a ListBox and I want to be able to click on multiple items and then drag the string from each of the selecteded items. I can get it working fine for a single object. I just can't get it working for more than 1.

I had a look at WPF Drag & drop from ListBox with SelectionMode Multiple but it completely stumped me (Tried to use it, but it didn't work and I didn't understand it.)

My current Drag 'n' Drop code is as follows.

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <EventSetter Event="MouseDoubleClick" Handler="ListBoxItemDClick"/>
        <EventSetter Event="MouseDown" Handler="ReferenceList_PreviewMouseLeftButtonDown"/>
        <EventSetter Event="MouseMove" Handler="ReferenceList_PreviewMouseMove"/>
        <EventSetter Event="MouseUp" Handler="ReferenceListMouseUp"/>
        <Style.Triggers>
            ...
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

Private Sub ListBoxItemDClick(sender As Object, e As MouseButtonEventArgs) 'DoubleClick
    Dim PW As MainWindow = Window.GetWindow(MainPage)
    If PW IsNot Nothing Then
        Dim selite As ListBoxItem = DirectCast(ReferenceList.ItemContainerGenerator.ContainerFromItem(ReferenceList.SelectedItem), ListBoxItem) 
        PW.NewItem = False
        PW.EditItem = ReferenceList.SelectedItem
        PW.ChangeSlide(sender, 1)
    End If
End Sub

Private Sub ReferenceList_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) 'Mouse Down on ListBoxItem
    Dim PW As MainWindow = Window.GetWindow(MainPage)
    StartPoint = e.GetPosition(Nothing)
    PW.Resizing = False
End Sub

Private Sub ReferenceListMouseUp(sender As Object, e As MouseButtonEventArgs) 'Mouse Up on ListBoxItem
    Dim PW As MainWindow = Window.GetWindow(MainPage)
    StartPoint = e.GetPosition(Nothing)
    PW.Resizing = True
End Sub

Private Sub ReferenceList_PreviewMouseMove(sender As Object, e As MouseEventArgs) 'MouseMove on ListBoxItem
    Dim PW As MainWindow = Window.GetWindow(MainPage)
    If PW.Resizing = False Then
        If ReferenceList.SelectedItems.Count = 1 Then
            Dim MousePosition As Point = e.GetPosition(Nothing)
            Dim Difference As Vector = StartPoint - MousePosition
            Dim StopDrop As Boolean = False
            Dim LBITEM As ListBoxItem = DirectCast(sender, ListBoxItem)
            If e.LeftButton = MouseButtonState.Pressed AndAlso (Math.Abs(Difference.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(Difference.Y) > SystemParameters.MinimumVerticalDragDistance) Then
                PW.TempItem = LBITEM.DataContext
                Dim FN As String = PW.TempItem.PropLastName & ", " & PW.TempItem.PropFirstName.Substring(0, 1)
                Dim TT As String = PW.TempItem.PropTitle
                Dim YR As String = PW.TempItem.PropYear.ToString
                Dim ReferenceText As String = FN & " " & YR & ", " & TT
                Dim DragData As DataObject = New DataObject(DataFormats.StringFormat, ReferenceText)
                If DragData IsNot Nothing And StopDrop = False Then
                    DragDrop.DoDragDrop(sender, DragData, DragDropEffects.Copy)
                End If
            End If
        Else
            Dim TotalReference As String = "Reference Data"
            For Each ITEM As Object In ReferenceList.SelectedItems
                Dim MousePosition As Point = e.GetPosition(Nothing)
                Dim Difference As Vector = StartPoint - MousePosition
                Dim StopDrop As Boolean = False
                If e.LeftButton = MouseButtonState.Pressed AndAlso (Math.Abs(Difference.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(Difference.Y) > SystemParameters.MinimumVerticalDragDistance) Then
                    PW.TempItem = ITEM
                    Dim FN As String = PW.TempItem.PropLastName & ", " & PW.TempItem.PropFirstName.Substring(0, 1)
                    Dim TT As String = PW.TempItem.PropTitle
                    Dim YR As String = PW.TempItem.PropYear.ToString
                    Dim ReferenceText As String = FN & " " & YR & ", " & TT
                    If TotalReference Is "Reference Data" Then
                        TotalReference = ReferenceText
                    Else
                        TotalReference = ReferenceText & vbCr & ReferenceText
                    End If
                End If
                Dim DragData As DataObject = New DataObject(DataFormats.StringFormat, TotalReference)
                If DragData IsNot Nothing And StopDrop = False Then
                    DragDrop.DoDragDrop(sender, DragData, DragDropEffects.Copy)
                End If
            Next
        End If
    End If
End Sub

Variables:

PW.Resizing (Boolean on MainWindow that determines whether window is resizing (True/False)
PW.TempItem (Declared on MainWindow as: Public TempItem as Reference 'Reference is my own class like a Student or such class to store data)
Private StartPoint As Point
ReferenceList is the ListBox containing all the ListBoxItems.

If I need to give more details please let me know. I can't tell what is wrong with my code for multiple items being dragged.

1

1 Answers

1
votes

Never mind, I found my error. I replaced this line:

TotalReference = ReferenceText & vbCr & ReferenceText

With this line:

TotalReference = TotalReference & vbCr & ReferenceText

And its all working now!