1

私は2つのリストビュー[listView1、listLocal]を持っています。それらはローカルPCとリモートPCの2つのファイルエクスプローラーで、
アイテムをドラッグした同じリストビューにドロップしている場合、コピー/移動する必要があります。そうでなければ、アイテムを他のリストビューにドロップしている場合は、送受信する必要があります。

したがって、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 に答える