ObjectListView 忽略拖动效果

ObjectListView is ignoring Dragging effect

您好,我正在为我的应用程序使用 ObjectListView。我试着做一个花哨的拖动效果,因为原来的蓝色效果不太好。这就像我拖动时突出显示第一列。我在我的普通列表视图中使用它

        private void lvPlaylist_DragOver(object sender, DragEventArgs e)
        {
            Point mLoc = lvPlaylist.PointToClient(Cursor.Position);
            var hitt = lvPlaylist.HitTest(mLoc);
            if (hitt.Item == null) return;

            int idx = hitt.Item.Index;
            if (idx == prevItem) return;

            lvPlaylist.Refresh();
            using (Graphics g = lvPlaylist.CreateGraphics())
            {
                Rectangle rect = lvPlaylist.GetItemRect(idx);
                Pen MyPen = new Pen(Color.OrangeRed, 3);
                g.DrawLine(MyPen, rect.Left, rect.Top, rect.Right, rect.Top);
            }
            prevItem = idx;
        }

但它在 ObjectListView 中不起作用。实际上确实如此,但是当我停止拖动但没有释放我的拖动对象时,它向我显示了蓝色默认拖动效果,同时我继续移动我看到了我自己的拖动效果。有什么方法可以禁用OLV拖动效果吗?

您的解决方案画了一条线 above/below 正在选择的项目?

您可以允许在使用之间删除:

lvPlaylist.IsSimpleDropSink = true;
((SimpleDropSink)lvPlaylist.DropSink).CanDropBetween = true;

如果这还不够好,您可以回复 ModelCanDrop 例如

//((SimpleDropSink)lvPlaylist.DropSink).ModelCanDrop+= ModelCanDrop;

 private void ModelCanDrop(object sender, ModelDropEventArgs e)
 {
     e.DropSink.Billboard.BackColor = Color.GreenYellow;
     e.DropSink.FeedbackColor = Color.GreenYellow;
     e.InfoMessage = "Hey there";
     e.Handled = true;
     e.Effect = DragDropEffects.Move;
 }

如果你真的那么讨厌它,你甚至可以:

e.DropSink.EnableFeedback = false;

ObjectListView 站点有一个关于拖放的非常深入的教程:

http://objectlistview.sourceforge.net/cs/blog4.html#blog-rearrangingtreelistview

如果你想做一些非常有趣的事情,你可以为 SimpleDropSink 编写自己的子类:

lvPlaylist.IsSimpleDragSource = true;
lvPlaylist.DropSink = new MyDropSink();

private class MyDropSink : SimpleDropSink
{
    public override void DrawFeedback(Graphics g, Rectangle bounds)
    {
        if(DropTargetLocation != DropTargetLocation.None)
            g.DrawString("Heyyy stuffs happening",new Font(FontFamily.GenericMonospace, 10),new SolidBrush(Color.Magenta),bounds.X,bounds.Y );
    }
}

对于你想要的行为,你应该尝试这样的事情:

private class MyDropSink : SimpleDropSink
{
    private ObjectListView _olv;

    public MyDropSink(ObjectListView olv)
    {
        _olv = olv;
    }

    public override void DrawFeedback(Graphics g, Rectangle bounds)
    {
        if(DropTargetLocation != DropTargetLocation.None)
        {
            Point mLoc = _olv.PointToClient(Cursor.Position);
            var hitt = _olv.HitTest(mLoc);
            if (hitt.Item == null) return;

            int idx = hitt.Item.Index;
            Rectangle rect = _olv.GetItemRect(idx);
            Pen MyPen = new Pen(Color.OrangeRed, 3);
            g.DrawLine(MyPen, rect.Left, rect.Top, rect.Right, rect.Top);
        }
    }
}