从一个ListView拖放项目到另一个ListView

7

现在,我可以从listView 1拖动项目到listView 2。如何克隆/复制/移动项目的数据?这里是我的意思的gif动画

widgetList就是listView1。也就是最右边的列表。

private void fillWidgetList()
    {
        widgetList.Groups.Add(new ListViewGroup("System", HorizontalAlignment.Left));

        var cpu = new ListViewItem { Text = "CPU", Tag = "", Group = widgetList.Groups["System"] };
        var ram = new ListViewItem { Text = "RAM", Tag = "", Group = widgetList.Groups["System"] };
        widgetList.Items.Add(cpu);
        widgetList.Items.Add(ram);
    }

widgetCollectionList是listView2,也就是中间的列表。

private void widgetList_ItemDrag(object sender, ItemDragEventArgs e)
    {
        DoDragDrop(e.Item, DragDropEffects.Move);
        // am i suppose to save the dragged item somewhere?
    }

    private void widgetCollectionList_DragEnter(object sender, DragEventArgs e)
    {
        //e.Effect = DragDropEffects.Copy;

        if (e.Data.GetDataPresent(typeof(ListViewItem)))
        {
            e.Effect = DragDropEffects.Move;
        }
    }

    private void widgetCollectionList_DragDrop(object sender, DragEventArgs e)
    {
        widgetCollectionList.Items.Add(e.Data.ToString()); // What do i replace this with?
    }

    private void WidgetMaker_Load(object sender, System.EventArgs e)
    {
        widgetCollectionList.AllowDrop = true;
        widgetCollectionList.DragDrop += new DragEventHandler(widgetCollectionList_DragDrop);
    }

1
一个 LVI 只能属于一个 LV。所以要移动,只需添加它。要复制,必须克隆它。由于您将 LVI 放在剪贴板上,因此应在 DragDrop 事件中将其转换回来。考虑使用 LVI 集合,这样您就可以一次移动多个。 - Ňɏssa Pøngjǣrdenlarp
将拖动的项目克隆到List<ListViewItem> copiedItems = new List<ListViewItem>();,然后在dragdrop事件中克隆该项,是否可行?如果是这样,我该如何从ItemDrag事件获取该项并添加到列表中?它会抛出“无法将对象转换为listviewitem”的错误。 - Snazzie
你想做什么:a)移动,b)复制,c)根据用户选择?e.Item将包含您在“DoDragDrop”中放置的任何内容。 - Ňɏssa Pøngjǣrdenlarp
移动将是理想的。 - Snazzie
1个回答

7
您离成功不远了。您没有将传递给e.Data的对象转换为LVI,而LVI对象只能属于一个ListView。因此,要移动它们,您需要先从旧位置删除它们;要复制它们,您需要克隆它们。(Groups 使这更加有趣:蔬菜类项目可以被拖放到水果组吗?)
我扩展了它,以便可以移动所有选定的项目,因此可以一次移动多个项目。如果您不需要这样,那么很容易将其删除。
private void lv_ItemDrag(object sender, ItemDragEventArgs e)
{
    // create array or collection for all selected items
    var items = new List<ListViewItem>();
    // add dragged one first
    items.Add((ListViewItem)e.Item);
    // optionally add the other selected ones
    foreach (ListViewItem lvi in lv.SelectedItems)
    {
        if (!items.Contains(lvi))
        {
            items.Add(lvi);
        }
    }
    // pass the items to move...
    lv.DoDragDrop(items, DragDropEffects.Move);
}

// this SHOULD look at KeyState to disallow actions not supported
private void lv2_DragOver(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(List<ListViewItem>)))
    {
        e.Effect = DragDropEffects.Move;
    }
}

private void lv2_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(List<ListViewItem>)))
    {
        var items = (List<ListViewItem>)e.Data.GetData(typeof(List<ListViewItem>));
        // move to dest LV
        foreach (ListViewItem lvi in items)
        {
            // LVI obj can only belong to one LVI, remove
            lvi.ListView.Items.Remove(lvi);
            lv2.Items.Add(lvi);
        }
    }
}

1
太棒了,正是我在寻找的。又增加了一项知识到我的知识库中。谢谢 :) - Snazzie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接