1

我有两个列表视图 [listView1,listLocal],它们是本地 PC 和远程 PC 的两个文件资源管理器,
如果我将项目拖放到我从中拖动它们的同一个列表视图中,它应该复制/移动。否则,如果我将项目放入另一个列表视图,那么它应该发送/接收。

所以让 listLocal 是本地 PC 的文件资源管理器,而 listView1 是远程 PC。
所以我需要一个条件来检查我是否将拖动的项目放到另一个列表视图中。

private void listLocal_ItemDrag(object sender, ItemDragEventArgs e)
    {
        if (_currAddress == null) return;   //if the current address is My Computer
        _DraggedItems.Clear();
        foreach (ListViewItem item in listLocal.SelectedItems)
        {
            _DraggedItems.Add((ListViewItem)item.Clone());
        }
        // if ( some condition )   call the same listview **listLocal.DoDragDrop**
        listLocal.DoDragDrop(e.Item, DragDropEffects.All);
        // else                    call the other listview
        listView1.DoDragDrop(e.Item, DragDropEffects.All);
    }

同样,当一个列表视图 DragDrop 被触发时,我需要检查拖动的项目是否来自同一个列表视图。

private void listView1_DragDrop(object sender, DragEventArgs e)
    {
        Point p = listView1.PointToClient(MousePosition);
        ListViewItem targetItem = listView1.GetItemAt(p.X, p.Y);
        // if ( some condition )     
        //here i need to check if the dragged item came from the same listview or not
        {
            if (targetItem == null)
            {
                PreSend(currAddress);          //send to the current address
            }
            else
            {
                PreSend(targetItem.ToolTipText);        //send to the target folder
            }
            return;
        }
        //otherwise

        if (targetItem == null) { return; }          //if dropped in the same folder return

        if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move)
        {
            Thread thMove = new Thread(unused => PasteFromMove(targetItem.ToolTipText, DraggedItems));
            thMove.Start();
        }
        else
        {
            Thread thCopy = new Thread(unused => PasteFromCopy(targetItem.ToolTipText, DraggedItems));
            thCopy.Start();
        }
    }
4

1 回答 1

2

您需要实现 DragEnter 事件以验证是否正在拖动正确的对象。您可以使用 ListViewItem.ListView 属性来验证它是否来自另一个 ListView。像这样:

    private void listView1_DragEnter(object sender, DragEventArgs e) {
        // It has to be a ListViewItem
        if (!e.Data.GetDataPresent(typeof(ListViewItem))) return;
        var item = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
        // But not one from the same ListView
        if (item.ListView == listView1) return;
        // All's well, allow the drop
        e.Effect = DragDropEffects.Copy;
    }

在 DragDrop 事件处理程序中不需要进一步检查。

于 2012-04-29T11:19:05.477 回答