Detecting a DragDrop onto a TreeView item

我与影子孤独终老i 提交于 2019-12-13 04:33:08

问题


I am doing a drag drop from one user control onto a different TreeView. However, how can I detect the drop into the tree view item? I can detect if there is a drop into the TreeView, but thats not the item. I can do an TreeViewItem.Drop event, but thats for when I drop an Item inside the TreeView, not from another control.

I tried seeing the TreeView to be focused, however, that did not solve it. I can detect DragEnter/Leave on the TreeView and it's Items, but not the drop. I have taken a look at other topics that said to have a DragOver to potentially fix this, hwever, but that did not work.


回答1:


Try this:

    private void treeView1_DragDrop(object sender, DragEventArgs e)
    {
        Point DropXY = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
        TreeNode DestinationNode = ((TreeView)sender).GetNodeAt(DropXY);

        MessageBox.Show(DestinationNode.Text);
    }

[EDIT] Note: You must have the AllowDrop property of the TreeView set to true. And, you must handle this event:

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


来源:https://stackoverflow.com/questions/7988646/detecting-a-dragdrop-onto-a-treeview-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!