WPF的ListView数据绑定拖/放自动滚屏 [英] WPF ListView Databound Drag/Drop Auto Scroll

查看:185
本文介绍了WPF的ListView数据绑定拖/放自动滚屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这里一会儿跟比的解决方案工作,并发现它非常有帮助。现在的问题我遇到的是当我拖正掉落的物品中或其他ListView控件,我想十一五期间的拖累(从指数30移动的项目索引1),它不是发生在向上/向下滚动。我将不得不拖动到在ListView视觉项目的顶部,手动滚动起来,然后再拖动,最终在我想要的位置结束。这是不是很人性化。

I've been working with Bea's solution here for a while and finding it very helpful. Problem now I'm having is when I drag-n-drop items within or to another ListView control and I want to scroll up/down "during" the drag (moving an item from index 30 to index 1), it's not happening. I would have to drag to the top of the visual items in the ListView, manually scroll up, then drag again, eventually ending at the position I want. This isn't very user friendly.

现在我发现,我想要做这项目正在拖过测试功能(DragDropHelper.DropTarget_ previewDragOver),而我得到了。

Now I found the function (DragDropHelper.DropTarget_PreviewDragOver) that I would want to do the testing of which item is being dragged over, and I'm getting that.

Dim pt As Point = e.GetPosition(DirectCast(Me.targetItemsControl, UIElement))

' Perform the hit test against a given portion of the visual object tree.
Dim result As HitTestResult = VisualTreeHelper.HitTest(Me.targetItemsControl, pt)

现在从那里我能得到这个视觉命中的DependencyProperty

Now from there I can get the DependencyProperty of this visual hit

Dim lvi As ListViewItem = TryCast(GetDependencyObjectFromVisualTree(TryCast(result.VisualHit, DependencyObject), GetType(ListViewItem)), ListViewItem)

这是一个ListViewItem的的。现在,在功能DropTarget_ previewDragOver我有DraggedItem,这是在BEA的例子类型的图片,但可以根据你绑定到ListView中的ObservableCollection改变。现在,我想拖动ListView的向上或向下取决于鼠标的位置上的控制。我试图用低于未完成非工作code

Which is of a ListViewItem. Now in the function DropTarget_PreviewDragOver I have the "DraggedItem" which is of type Picture in Bea's example, but that can change depending on the ObservableCollection you have bound to the ListView. Now, I want to drag the ListView up or down depending on where the mouse is on the control. I've attempted with the below un-finished non-working code

If lvi IsNot Nothing Then
    If pt.Y <= 25 Then
        Dim lv As ListView = TryCast(targetItemsControl, ListView)
        If lv IsNot Nothing Then
            Dim index As Integer = lv.Items.IndexOf(lvi)
            If index > 1 Then
                lv.ScrollIntoView(lv.Items(index - 1))
            End If
        End If
    Else
        If pt.Y >= Me.targetItemsControl.ActualHeight - 25 Then
            Debug.Print("Scroll Down")
        End If
    End If
End If

有人能指出我在正确的方向得到这个ItemsControl的或ListView滚动拖动过的项目时??

Can someone point me in the right direction to get this ItemsControl or ListView to scroll when dragging over the items??

谢谢!

推荐答案

我还在这个相同的问题瞎​​搞了。我使用BEA的拖放略加修改的版本和Drop发现 < /一>,它是在VB而不是C#。当我用ScrollIntoView如上所述,我可以向下滚动,但是不起来。所以我搞砸周围,以此为我DropTarget_ previewDragOver想出了:

I'm still messing around with this exact same issue too. I'm using a slightly modified version of Bea's Drag and Drop found here, which is in VB instead of C#. When I used ScrollIntoView as described above, I could scroll down but not up. So I messed around and came up with this as my DropTarget_PreviewDragOver:

 Private Sub DropTarget_PreviewDragOver(ByVal sender As Object, ByVal e As DragEventArgs)
        Dim draggedItem As Object = e.Data.GetData(Me.m_format.Name)
        Me.DecideDropTarget(e)
        If (Not draggedItem Is Nothing) Then
            If (TypeOf m_targetItemsControl Is ListBox) Then
                Dim lb As ListBox = CType(m_targetItemsControl, ListBox)
                Dim temp As Integer = m_insertionIndex
                Dim scroll As ScrollViewer = Utilities.GetScrollViewer(lb)
                If scroll.VerticalOffset = temp Then
                    temp -= 1
                End If
                If temp >= 0 And temp <= (lb.Items.Count - 1) Then
                    lb.ScrollIntoView(lb.Items(temp))
                End If
            End If
            Me.ShowDraggedAdorner(e.GetPosition(Me.m_topWindow))
            Me.UpdateInsertionAdornerPosition()
        End If
        e.Handled = True
    End Sub

和我有包括这个实用功能,从这里采取

and I had to include this utility function, taken from here

    Public Shared Function GetScrollViewer(ByVal listBox As ListBox)
    Dim scroll_border As Decorator = CType(VisualTreeHelper.GetChild(listBox, 0), Decorator)
    If (TypeOf scroll_border Is Decorator) Then
        Dim scroll As ScrollViewer = CType(scroll_border.Child, ScrollViewer)
        If (TypeOf scroll Is ScrollViewer) Then
            Return scroll
        Else
            Return Nothing
        End If
    Else
        Return Nothing
    End If


End Function

这是伟大的和所有。然后在运行什么theuberk上面装饰器移动所提到的,在使这个容易被别人后来的精神,我加入到DragDropAdorner类变量:

which is great and all. Then running out what theuberk mentioned above with the adorner moving, and in the spirit of making this easy for someone else later, I added a variable to the DragDropAdorner class:

    Private m_mouseDelta As Point

添加到了DragSource_ previewMouseLeftButtonDown的最后一行:

Added this to the last line of DragSource_PreviewMouseLeftButtonDown:

        Me.m_mouseDelta = e.GetPosition(m_sourceItemContainer)

和转身ShowDraggedAdorner为:

And turned ShowDraggedAdorner into:

    Private Sub ShowDraggedAdorner(ByVal currentPosition As Point)
    If (Me.m_draggedAdorner Is Nothing) Then
        Dim adornerLayer As AdornerLayer = adornerLayer.GetAdornerLayer(Me.m_topWindow.Content)
        Me.m_draggedAdorner = New DraggedAdorner(Me.m_draggedData, DragDropBehavior.GetDragTemplate(Me.m_sourceItemsControl), m_topWindow.Content, adornerLayer)
    End If
    Me.m_draggedAdorner.SetPosition((currentPosition.X - m_mouseDelta.X), (currentPosition.Y - m_mouseDelta.Y))
    End Sub

这篇关于WPF的ListView数据绑定拖/放自动滚屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆