Dev TreeList 實現節點拖拽功能


實現邏輯,winform程序Dev TreeList控件上下級拖拽,選中節點可以拖拽至他的同級節點或上級節點,不可拖拽至他的下級幾點

如下圖:m01節點可以拖拽至root節點外面或者my節點下,但不可拖拽至m01-1和m01-2節點下

實現關鍵代碼:

treeList.AllowDrop =true; //是否允許拖拽

需要的五個事件

詳細代碼:

        //移過組件
        private void treeListFunc_MouseMove(object sender, MouseEventArgs e)
        {
            TreeList treelist = sender as TreeList;
            if (e.Button == MouseButtons.Left && downHitInfo != null)
            {
                if (treeListFunc.Selection.Count == 0)
                    return;
                Size dragSize = SystemInformation.DragSize;
                Rectangle dragRect = new Rectangle(new Point(downHitInfo.MousePoint.X - dragSize.Width / 2,
                    downHitInfo.MousePoint.Y - dragSize.Height / 2), dragSize);

                if (!dragRect.Contains(new Point(e.X, e.Y)))
                {
                    List<TreeListNode> node = new List<TreeListNode>();
                    foreach (TreeListNode n in treeListFunc.Selection)
                    {
                        node.Add(n);
                    }
                    treelist.DoDragDrop(node, DragDropEffects.Copy);
                    downHitInfo = null;
                    DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
                }
            }

        }
        //按下鼠標
        private void treeListFunc_MouseDown(object sender, MouseEventArgs e)
        {
            TreeList treelist = sender as TreeList;
            downHitInfo = null;
            TreeListHitInfo hitInfo = treelist.CalcHitInfo(new Point(e.X, e.Y));

            if (Control.ModifierKeys != Keys.None) return;
            if (e.Button == MouseButtons.Left)
            {
                downHitInfo = hitInfo;
            }
        }
        //拖至邊界
        private void treeListFunc_DragOver(object sender, DragEventArgs e)
        {
            TreeList treelist = sender as TreeList;
            if (treelist != null)
            {
                e.Effect = DragDropEffects.Copy;
            }
        }
        //拖放時
        private void treeListFunc_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }
        //拖放完成
        private void treeListFunc_DragDrop(object sender, DragEventArgs e)
        {
            List<TreeListNode> nodes = (List<TreeListNode>)e.Data.GetData(typeof(List<TreeListNode>));
            TreeList grid = sender as TreeList;
            DataTable table = grid.DataSource as DataTable;

            if (nodes != null && nodes.Count > 0 && table != null)
            {
                //移動至節點
                TreeListHitInfo info = treeListFunc.CalcHitInfo(treeListFunc.PointToClient(new System.Drawing.Point(e.X, e.Y)));
                TreeListNode nodeTo = info.Node;

                foreach (TreeListNode node in nodes)
                {
                    string currCode = node.GetValue("Code").ConvertTo("");
                    var dr1 = dsMain.Tables[0].Select().Where(a => a["Code"].ConvertTo("") == currCode);

                    DataRow[] dr = dsMain.Tables[0].Select($"Code='{currCode}'");
                    if (dr.Length >= 1)
                    {
                        //拖放節點未找到
                        if (nodeTo == null)
                        {
                            dr[0]["Upper_ID"] = Guid.Empty.ToString();
                            dr[0]["Upper_Code"] = "";
                            dr[0]["Upper_Name"] = "";
                        }
                        else
                        {
                            string currCodeTo = nodeTo.GetValue("Code").ConvertTo("");
                            //當不存在目標節點或目標節點的層級大於等於被拖動節點時(后者僅針對樹內拖拽),顯示禁止圖標
                            if (currCode == "" || currCode == currCodeTo || nodeTo.HasAsParent(node))
                            {
                                e.Effect = DragDropEffects.None;
                                return;
                            }
                            dr[0]["Upper_ID"] = nodeTo.GetValue("ID").ConvertTo(Guid.Empty.ToString());
                            dr[0]["Upper_Code"] = nodeTo.GetValue("Code").ConvertTo(Guid.Empty.ToString());
                            dr[0]["Upper_Name"] = nodeTo.GetValue("Name").ConvertTo(Guid.Empty.ToString());
                        }
                        PageHelper.SetMenuImage(treeListFunc);
                    }
                }
            }
        }

最終效果展示:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM