0

我有一个ListBox,我希望能够单击多个项目,然后拖到另一个程序中。我可以让它对单个对象正常工作。我只是不能让它工作超过1。

我用 SelectionMode Multiple 从 ListBox 中查看了WPF Drag & drop但它完全让我难过(尝试使用它,但它没有用,我不明白。)

我当前的 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.

如果我需要提供更多详细信息,请告诉我。我无法判断拖动多个项目的代码有什么问题。

4

1 回答 1

0

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

TotalReference = ReferenceText & vbCr & ReferenceText

有了这条线:

TotalReference = TotalReference & vbCr & ReferenceText

现在一切正常!

于 2013-01-03T07:06:55.807 回答