3

我正在开发一个 Wpf 项目,但现在我遇到了一个ListView问题。

事实证明,我已经Drag&Drop在 ListView 上实现了一个运行良好的功能。当我尝试向下或向上滚动时,问题就来了。通过这样做,Drag&Drop功能被激活,阻止我继续滚动。

我发现这个解决方案表明我们需要将控件附加到ScrollChanged事件。

<ListView ScrollViewer.ScrollChanged="listView1_ScrollChanged"...

但我真的不知道在那个处理程序中该做什么。我怎么能从那个事件中禁用拖放?我怎样才能再次启用它?或者,有没有更好的方法来解决这个问题?

那是我的Drag&Drop代码:

 private void listView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Store the mouse position
        startPoint = e.GetPosition(null);
    }

    private void listView1_MouseMove(object sender, MouseEventArgs e)
    {
        // Get the current mouse position
        Point mousePos = e.GetPosition(null);
        Vector diff = startPoint - mousePos;

        if (e.LeftButton == MouseButtonState.Pressed &&
            (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
            Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
        {
            // Get the dragged ListViewItem
            ListView listView = sender as ListView;

            // Get items to drag
            var a = listView.SelectedItems;

            // Initialize the drag & drop operation
            DataObject dragData = new DataObject("myFormat", a);
            DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Move);
        } 
    }

提前致谢。

4

3 回答 3

0

I use a simple method to confirm whether the drag operation is confirmed or not using the SystemParameters.MinimumHorizontalDragDistance and SystemParameters.MinimumVerticalDragDistance that were made for this purpose:

private bool IsDragConfirmed(Point point)
{
    bool horizontalMovement = Math.Abs(point.X - dragStartPosition.X) > 
         SystemParameters.MinimumHorizontalDragDistance;
    bool verticalMovement = Math.Abs(point.Y - dragStartPosition.Y) > 
         SystemParameters.MinimumVerticalDragDistance;
    return (horizontalMovement | verticalMovement);
}

It is called in the PreviewMouseMove event... here is a simplified example:

private void DragSourcePreviewMouseMove(object sender, MouseEventArgs e)
{
    if (isMouseDown && IsDragConfirmed(e.GetPosition(sender as ListBox)))
    {
        // Start your drag operation here
    }
}
于 2013-10-16T09:00:05.817 回答
0

如果您不在 MouseDown 事件中,您可以阻止 MouseMove 事件:

bool stopDrag = true;
     private void listView1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // Store the mouse position
            startPoint = e.GetPosition(null);
stopDrag = false;
        }

        private void listView1_MouseMove(object sender, MouseEventArgs e)
        {
if(stopDrag)
   return;

            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = startPoint - mousePos;

            if (e.LeftButton == MouseButtonState.Pressed &&
                (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
                // Get the dragged ListViewItem
                ListView listView = sender as ListView;

                // Get items to drag
                var a = listView.SelectedItems;

                // Initialize the drag & drop operation
                DataObject dragData = new DataObject("myFormat", a);
                DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Move);
            } 
        }

private void listView1_MouseUp(...)
{
stopDrag = true;
}

这应该可以,我是在浏览器中写的,所以如果我做错了什么,请原谅,对于格式,我希望你明白。

于 2013-10-15T21:28:06.653 回答
0

我有与下面的答案相同的问题和相同的来源。好吧,如果鼠标位置不在列表视图项目上方,我决定防止拖动(矩形 0、0、lv.width - scroll.width、lv.height - scroll.height)所以我有一个“allowdrag”标志,它变成如果在 PreviewMouseLeftButtonDown 过去时鼠标指针位于必要的矩形上方,则为真。

VB.NET

Private allowdrag As Boolean

Private Sub lv_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles lvAttributesDefault.PreviewMouseLeftButtonDown
    Dim listView As ListView = TryCast(sender, ListView)
    allowdrag = e.GetPosition(sender).X < listView.ActualWidth - SystemParameters.VerticalScrollBarWidth And e.GetPosition(sender).Y < listView.ActualHeight - SystemParameters.HorizontalScrollBarHeight
End Sub

Private Sub lv_MouseMove(sender As Object, e As MouseEventArgs) Handles lvAttributesDefault.MouseMove
    Dim listView As ListView = TryCast(sender, ListView)

    If e.LeftButton = MouseButtonState.Pressed And listView.SelectedItem IsNot Nothing And allowdrag Then
        Dim obj As clsAttribute = CType(listView.SelectedItem, clsAttribute)
        Dim dragData As New DataObject("clsAttribute", obj)

        DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Copy)
    End If
End Sub

C# (telerik 代码转换器)

private bool allowdrag;
private void lv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    ListView listView = sender as ListView;
    allowdrag = e.GetPosition(sender).X < listView.ActualWidth - SystemParameters.VerticalScrollBarWidth & e.GetPosition(sender).Y < listView.ActualHeight - SystemParameters.HorizontalScrollBarHeight;
}

private void lv_MouseMove(object sender, MouseEventArgs e)
{
    ListView listView = sender as ListView;

    if (e.LeftButton == MouseButtonState.Pressed & listView.SelectedItem != null & allowdrag) {
        clsAttribute obj = (clsAttribute)listView.SelectedItem;
        DataObject dragData = new DataObject("clsAttribute", obj);

        DragDrop.DoDragDrop(listView, dragData, DragDropEffects.Copy);
    }
}

PS。clsAttribute 是我的自定义类。列表视图绑定到 ObservableCollection(Of clsAttribute)

于 2017-02-19T13:48:03.037 回答