开发者

How to Get Listbox Selected Item in DragOver Event

I Have two listboxes when I drag One item from the source listbox I would like for the destination listbox to select the item based off the mouse position over the listbox. I have this implemented but the MouseOver event only fires when you are not dragging. How do I select the item based off the dragOver invent of the listbox.

Here is the code

 Private Sub CasesBox2_DragEnter(sender As Object, e As System.Windows.DragEventArgs) Handles CasesBox2.DragEnter
    CasesBox2.CaptureMouse()
    If Not e.Data.GetDataPresent("contact") OrElse sender = e.Source Then
        e.Effects = DragDropEffects.None


    Else
        e.Effects = DragDropEffects.All
    End If


End Sub

Private Sub CasesBox2_DragOver(sender As Object, e As System.Windows.DragEventArgs) Handles CasesBox2.DragOver

    Dim MousePoint As Windows.Point = e.GetPosition(Nothing)

End Sub

Private Sub CasesBox2_Drop(sender As Object, e As System.Windows.DragEventArgs) Handles CasesBox2.Drop

    If e.Data.GetDataPresent("myFormat") Then
        Dim contact As Details = TryCast(e.Data.GetData("myFormat"), Details)
        Dim cse As Cases = CasesBox2.SelectedItem
        If cse IsNot Nothing Then
            Dim cs2 As ObservableCollection(Of Details) = DtlBox3.ItemsSource
            cs2.Remove(contact)
            cse.AddDetailsToCase(contact)
            IsDragInitiated = False
        End If
        IsDragInitiated = False
    End If
    IsDragInitiated = False

End Sub


Private Sub CasesBox2_SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles CasesBox2.SelectionChanged
    Try
        If IsDragInitiated Then

        Else
            Dim SelectedItem As Cases = CasesBox2.SelectedItem
            DtlBox3.ItemsSource = SelectedItem.CaseDetails
        End If



    Catch ex As Exception

    End Try

End Sub

Private Sub DtlBox3_MouseMove(sender As Object, e As System.Windows.Input.MouseEventArgs) Handles DtlBox3.MouseMove
    Try
        ' Get the current mouse position
        Dim mousePos As Windows.Point = e.GetPosition(Nothing)
        Dim diff As Vector = StartPoint - mousePos

        If e.LeftButton = MouseButtonState.Pressed AndAlso Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance OrElse Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance Then
            ' Get the dragged ListViewItem
            Dim listView As ListBox = TryCast(sender, ListBox)
            Dim listViewItem As ListBoxItem = FindAnchestor(Of ListBoxItem)(DirectCast(e.OriginalSource, DependencyObject))

            ' Find the data behind the ListViewItem
            Dim contact As Details = DirectCast(listView.ItemContainerGenerator.ItemFromContainer(listViewItem), Details)

            ' Initialize the drag & drop operation
            Dim dragData As New DataObject("myFormat", contact)
            IsDragInitiated = True
            DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move)


        End If
    Catch ex As Exception

    End Try


End Sub

' Helper to search up the VisualTree
Private Shared Function FindAnchestor(Of T As DependencyObject)(current As DependencyObject) As T
    Do
        If TypeOf current Is T Then
            Return DirectCast(current, T)
        End If
        current = VisualTreeHelper.GetPa开发者_运维百科rent(current)
    Loop While current IsNot Nothing
    Return Nothing
End Function

Private Sub DtlBox3_PreviewMouseLeftButtonDown(sender As Object, e As System.Windows.Input.MouseButtonEventArgs) Handles DtlBox3.PreviewMouseLeftButtonDown
    StartPoint = e.GetPosition(Nothing)
End Sub

Private Sub Border_MouseEnter(sender As System.Object, e As System.Windows.Input.MouseEventArgs)
    CasesBox2.SelectedItem = CType(sender, Border).DataContext
    If CasesBox2.IsFocused Then
    Else
        CasesBox2.Focus()
    End If
End Sub


Sounds like you're trying to do hot-tracking with DragOver on individual list items.

If you're using WPF, you could probably make a custom template for ListBoxItems that has a trigger for that.

If not: You could implement DragOver on the list items themselves instead of just the ListBox control, and you would need to maintain a reference to the currently hot-tracked item so you could un-highlight it. This means your ListBox has to be populated with some custom objects, though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜