简体   繁体   English

Devexpress将文件从资源管理器移动到TreeList

[英]Devexpress move file from explorer into TreeList

I'm trying to drag & drop files into my TreeList . 我正在尝试将文件拖放到TreeList I'm able to move files/folders which are inside the TreeList , but I also want to drag files from Windows Explorer into the folders of my TreeList . 我可以移动TreeList内的文件/文件夹,但我也想将文件从Windows资源管理器拖到TreeList的文件夹中。 The dragdrop event of the treeList is triggered but both nodes (dragged and target) are null. 触发treeListdragdrop事件,但两个节点(拖动和目标)均为空。 e.Data is not null for both nodes, e.Data is a DataObject . 两个节点的e.Data都不为空, e.Data是一个DataObject Maybe there's something wrong with the conversion to TreeList nodes, but this event works for drag & drop insde the TreeList . 转换为TreeList节点可能出了点问题,但是此事件适用于拖放到TreeList How can I solve the nodes problem? 如何解决节点问题?

This is the code snippet where the problem occurs: 这是发生问题的代码片段:

     private void treeList1_DragDrop(object sender, DragEventArgs e)
    {

        var draggedNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode; //null        
        var targetNode = Tree.ViewInfo.GetHitTest(Tree.PointToClient(new Point(e.X, e.Y))).Node; // null

        if (targetNode == null) return;

        if (draggedNode != null)
        {
            if (targetNode[treeListColumn3].ToString() == "File")
            {
                if (targetNode.ParentNode == draggedNode.ParentNode)
                    return;
                MoveInFolder(draggedNode, targetNode.ParentNode);
            }
            else
            {
                MoveInFolder(draggedNode, targetNode);
            }
            e.Effect = DragDropEffects.None;
        }
        else
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (files == null) return;

            //Do something with your TreeList
            var nodeTarget = Tree.ViewInfo.GetHitTest(Tree.PointToClient(new Point(e.X, e.Y))).Node;
            TreeListNode node;
            FileInfo fi;
            foreach (string s in files)
            {

                fi = new FileInfo(s);
                node = Tree.AppendNode(new object[] { s, fi.Name, "File", fi.Length, fi }, nodeTarget);
                node.HasChildren = false;
                MoveInFolder(node, nodeTarget);
            }
        }

EDIT I editted my code to the code which works for me. 编辑我将代码编辑为对我有用的代码。

When you drag files from Windows Explorer you cannot convert it to TreeListNode object because you are draging object of another type. 从Windows资源管理器中拖动文件时,无法将其转换为TreeListNode对象,因为您正在拖动其他类型的对象。 You can only get the array of your files by using DataFormats.FileDrop as parameter for e.Data.GetData method and create/move/delete nodes in your TreeList according to files that you droped to it: 您只能通过使用DataFormats.FileDrop作为e.Data.GetData方法的参数来e.Data.GetData文件数组,并根据e.Data.GetData到其的文件在TreeList创建/移动/删除节点:

private void treeList1_DragDrop(object sender, DragEventArgs e)
{
    var draggedNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode; //null        
    var targetNode = Tree.ViewInfo.GetHitTest(Tree.PointToClient(new Point(e.X, e.Y))).Node; // null

    if (targetNode == null) return;

    if (draggedNode != null)
    {
        if (targetNode[treeListColumn3].ToString() == "File")
        {
            if (targetNode.ParentNode == draggedNode.ParentNode)
                return;
            MoveInFolder(draggedNode, targetNode.ParentNode);
        }
        else
        {
            MoveInFolder(draggedNode, targetNode);
        }
        e.Effect = DragDropEffects.None;            
    }
    else
    {
        string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

        if (files == null) return;

        //Do something with your TreeList
    }
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM