コード例 #1
0
        private void TreeList1_DragDrop(object sender, DragEventArgs e)
        {
            TreeList        tl         = sender as TreeList;
            DXDragEventArgs args       = e.GetDXDragEventArgs(tl);
            TreeListNode    targetNode = args.TargetNode;

            // get treelist drag node. if null, was dragging from gridcontrol
            TreeListNode dragNode = e.Data.GetData(typeof(TreeListNode)) as TreeListNode;

            // dropping TreeListNode
            if (dragNode != null)
            {
                // get drag and target parents
                TreeListNode dragParent   = dragNode.ParentNode;
                TreeListNode targetParent = targetNode.ParentNode;

                int targetIndex = tl.GetNodeIndex(targetNode);
                int dragIndex   = tl.GetNodeIndex(dragNode);

                // if dropping a parent node
                if (dragParent == null)
                {
                    tl.MoveNode(dragNode, null, true, dragIndex < targetIndex ? targetIndex : targetIndex + 1);
                }

                // if dropping a child node
                else
                {
                    // if dropping child into same parent
                    if (targetParent == dragParent) //  && dragParent != null)
                    {
                        tl.SetNodeIndex(dragNode, dragIndex < targetIndex ? targetIndex : targetIndex + 1);
                    }

                    // if dropping child into different parent
                    else
                    {
                        // dropping on a child of other parent
                        if (targetParent != null && targetParent != dragParent)
                        {
                            tl.MoveNode(dragNode, targetParent, true, tl.GetNodeIndex(targetNode) + 1); // DX: +1 to insert AFTER
                        }
                        // dropping on other parent node
                        if (targetParent == null && targetNode.Level != dragNode.Level) // DX: Insert a node into a root node
                        {
                            tl.MoveNode(dragNode, targetNode, true, 0);
                        }
                    }
                }
                e.Effect = DragDropEffects.None;
            }

            // dropping GridObject
            else
            {
                EstimateProjectType pt   = args.Data.GetData(typeof(EstimateProjectType)) as EstimateProjectType;
                EstimateCalculator  calc = args.Data.GetData(typeof(EstimateCalculator)) as EstimateCalculator;
                if (pt == null && calc == null)
                {
                    return;
                }
                TreeListNode parent = null;
                string       description;
                if (pt != null)
                {
                    description = pt.Description;
                }
                else
                {
                    description = calc.Description;
                }

                TreeListNode newNode = tl.AppendNode(new object[] { description }, parent);
                if (pt == null)
                {
                    newNode.Tag = calc;
                }
                else
                {
                    newNode.Tag = pt;
                }

                if (targetNode != null)
                {
                    // dropping a CALCULATOR grid row onto root node, insert as first child
                    if (targetNode.Level == 0 && calc != null)
                    {
                        tl.MoveNode(newNode, targetNode, true, 0);
                    }

                    // dropping ANY type of grid row on any type of node, insert AFTER
                    else
                    {
                        tl.MoveNode(newNode, targetNode.ParentNode, true, tl.GetNodeIndex(targetNode) + 1);
                    }
                }
            }
            if (targetNode != null && targetNode.HasChildren)
            {
                targetNode.Expanded = true;
            }
            tl.Refresh();
        }