【问题标题】:Drag drop ListView Item from one listView to another将 ListView 项从一个 listView 拖放到另一个
【发布时间】:2016-11-04 20:57:31
【问题描述】:

现在,我可以将一个项目从 listView 1 拖到 listView 2。如何克隆/复制/移动项目的数据? Gif of what i mean here

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);
    }

【问题讨论】:

  • 一个LVI只能属于一个LV。所以要移动,你只需添加它。要复制,您必须克隆它。由于您将 LVI 放在剪贴板上,因此您应该在 DragDrop 事件中将其投射回。考虑使用一组 LVI,以便一次可以使用多个
  • 会将拖动的项目克隆到 List 复制项目 = new List();然后在拖放事件上克隆项目?如果是这样,我将如何从 ItemDrag 事件中获取项目到列表中?它的抛出无法将对象转换为 listviewitem
  • 你想做什么:a) 移动,b) 复制 c) 任一个,取决于用户? e.Item 将包含您在DoDragDrop 中放置的任何内容
  • 移动将是理想的

标签: c# .net winforms listview listviewitem


【解决方案1】:

你快到了。您没有将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);
        }
    }
}

【讨论】:

  • 太棒了,正是我想要的。另一个知识添加到我的知识库中。谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 2017-11-28
  • 2016-04-02
  • 1970-01-01
相关资源
最近更新 更多