【问题标题】:Visual aid in drag and drop within a ListView在 ListView 中拖放的视觉辅助
【发布时间】:2013-01-17 00:56:42
【问题描述】:

问题

我有两个 ListView。一个选项可以拖入另一个选项。这是“字段”ListView。另一个是“builder” ListView。我想不出一种方法来直观地向用户展示项目将被插入的位置。我想在 ListViewItem 之间画一条线以在视觉上帮助用户。

private void builder_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void fields_DragEnter(object sender, DragEventArgs e)
{
    e.Effect = DragDropEffects.Move;
}

private void fields_ItemDrag(object sender, ItemDragEventArgs e)
{
    fromBuilder = false;
    fields.DoDragDrop(e.Item, DragDropEffects.Move);
}

private void builder_ItemDrag(object sender, ItemDragEventArgs e)
{
    fromBuilder = true;
    builder.DoDragDrop(e.Item, DragDropEffects.Move);
}

private void builderAndFields_DragDrop(object sender, DragEventArgs e)
{
    ListViewItem i = new ListViewItem();
    i = e.Data.GetData(typeof(ListViewItem)) as ListViewItem;

    // Since this function works for both the builder and the fields,
    // we have to check to see where we are dropping, the sender
    // is the ListView we are dropping onto
    if (sender.Equals(builder))
    {
        ListViewItem c = new ListViewItem();
        c = (ListViewItem)i.Clone();
        Point cp = builder.PointToClient(new Point(e.X, e.Y));
        Console.WriteLine("cp: " + cp);
        ListViewItem dragToItem = builder.GetItemAt(cp.X, cp.Y);
        Console.WriteLine("dragToItem: " + dragToItem);
        int dropIndex = dragToItem.Index;
        // Now, we have to check to see if we are reordering or adding
        // So, we check the flag to see if the dragDrop was initiated 
        // on the builder or on the fields ListView
        if (fromBuilder)
        {
            builder.Items.Insert(dropIndex, c);
            builder.Items.Remove(i);
        }
        else
        {
            Console.WriteLine(dropIndex);
            builder.Items.Insert(dropIndex, c);
        }
    }
    // If the sender is the fields listView, the user is trying to remove
    // the item from the builder.
    else
    {
        builder.Items.Remove(i);
    }
}

【问题讨论】:

    标签: c# winforms listview drag-and-drop


    【解决方案1】:

    Better ListView 支持drop highlighting and insertion marks 开箱即用:

    我认为您也可以使用 DragOver 事件处理程序中的 HitTest() 方法在常规 ListView 中执行此操作。

    【讨论】:

    • 是的,我不想为这个功能支付那种现金。我会看看我能用 HitTest() 做什么。
    • 你也可以试试Better ListView Express,它是免费的。它附带了很多示例,包括插入标记和拖放的用法。
    【解决方案2】:

    看看PreviewDragEnterPreviewDragOverPreviewDragLeave

    您可以使用该事件将 Adorner 添加到您的下拉列表中。如果你搜索“WPF DragDropHelper”,你会发现几个详细的例子。

    【讨论】:

    • 据我所知,这不适用于 C# Windows 窗体应用程序。
    猜你喜欢
    • 2010-09-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多