从ListBox拖放多个项目

时间:2013-01-02 09:30:58

标签: .net wpf vb.net drag-and-drop listbox

我有一个ListBox,我希望能够点击多个项目,然后从每个选择的项目中拖动字符串。我可以让它适用于单个对象。我只是不能让它工作超过1。

我看了WPF Drag & drop from ListBox with SelectionMode Multiple,但它完全让我感到难过(试图使用它,但它不起作用,我不理解它。)

我当前的Drag'n'Drop代码如下。

<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

变量:

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.

如果我需要提供更多详情,请告诉我。我不知道我的代码有多少被拖动的问题。

1 个答案:

答案 0 :(得分:1)

没关系,我发现了我的错误。我替换了这一行:

TotalReference = ReferenceText & vbCr & ReferenceText

这一行:

TotalReference = TotalReference & vbCr & ReferenceText

现在一切正常!