2012-12-31 132 views
0

我有一個ListBox,我希望能夠點擊多個項目,然後拖入另一個程序。我可以很好地爲單個對象工作。我只是不能得到它的工作超過1.多個項目從列表框中拖放'n'拖放

我看了一下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. 

如果我需要提供更多的詳細信息,請讓我知道。我無法辨別我的代碼是否拖放了多個項目。

回答

0

沒關係,我發現我的錯誤。我換成這一行:

TotalReference = ReferenceText & vbCr & ReferenceText 

這一行:

TotalReference = TotalReference & vbCr & ReferenceText 

和它的所有工作現在!