C#中拖放放大器;滴在列表视图 [英] c# Drag & drop in listview

查看:143
本文介绍了C#中拖放放大器;滴在列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我从中拖动到ListView控件列表框。
现在我已经在ListView组,以便当从ListView中的项目它有添加该组下的ListViewGroup点被丢弃。

I've got a listbox from which I'm dragging into the ListView. Now I have groups in the ListView so when the item from the ListView is dropped at the point of the listviewgroup it has to add it under that group.

这是code它处理的下降。

This is the code which handles the drop.

    private void lstvPositions_DragDrop(object sender, DragEventArgs e)
    {

        var group = lstvPositions.GetItemAt(e.X, e.Y);
        var item = e.Data.GetData(DataFormats.Text).ToString();
        lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item});

    }

我没有找到可以给groupitem功能,所以我用话getItemAt从中我也有机会获得的ListViewGroup。

I didn't find a function that could give the groupitem, so I used GetItemAt from which I also have access to the listviewgroup.

但总是话getItemAt返回null。

But GetItemAt always returns null.

我是不是做错了什么?有没有更好的方式来做到这一点?

Am I doing something wrong? Is there a better way to accomplish this?

推荐答案

首先,我假设你正在使用一个ListView,而不是一个列表框,列表框为不包含话getItemAt成员。

First, I assume you're using a ListView, not a ListBox, as ListBox does not contain a GetItemAt member.

要解决你的问题,点转换为本地坐标:

To solve your problem, convert the point to local coordinates:

private void lstvPositions_DragDrop(object sender, DragEventArgs e)
{
   var localPoint = lstvPositions.PointToClient(new Point(e.X, e.Y));
   var group = lstvPositions.GetItemAt(localPoint.X, localPoint.Y);
   var item = e.Data.GetData(DataFormats.Text).ToString();
   lstvPositions.Items.Add(new ListViewItem {Group = group.Group, Text = item});
}

这篇关于C#中拖放放大器;滴在列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆