Dev TreeList 實現節點拖拽功能

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

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

實現關鍵代碼:orm

treeList.AllowDrop =true; //是否容許拖拽事件

須要的五個事件string

詳細代碼:it

        //移過組件
        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);io

                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;
                }
            }table

        }
        //按下鼠標
        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));form

            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;object

            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);
                    }
                }
            }
        }

最終效果展現:

相關文章
相關標籤/搜索