zoukankan      html  css  js  c++  java
  • DevExpress之TreeList用法

    在程序开发过程中,TreeList控件的使用频率还是挺高的!本文对一些常用算法进行了一些总结:

    1.TreeList数据绑定

        TreeList数据绑定采用数据源方式的绑定 

          TreeList.DataSource=List集合,DataTable,DataSet

     绑定之前设置2个基本的绑定属性:KeyFieldName和ParentFieldName

        DataSource为对应 的DataTable,指定KeyFieldName为表主键字段,ParentFieldName为表指向主键的外键字段名。

    2.TreeList节点

    //鼠标单击节点时,获取节点信息
    TreeListHitInfo hitinfo = emTreeList.CalcHitInfo(emTreeList.PointToClient(new Point(e.X, e.Y)));
    TreeListNode node = hitinfo.Node;//当前点击的节点

    //获取emTreeList控件中第一个节点中name的值

    emTreeList.Nodes[0].GetValue("name")

    //点击节点前触发的时间

    privatevoid treeLstModuleAction_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
    {
    e.State
    = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
    }

    //点击节点后触发的时间

    privatevoid treeLstModuleAction_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
    {
    SetCheckedChildNodes(e.Node, e.Node.CheckState);
    SetCheckedParentNodes(e.Node, e.Node.CheckState);
    }

     


    /// 选择子节点时触发
    privatevoid SetCheckedChildNodes(TreeListNode node, CheckState check)
    {
    for (int i =0; i < node.Nodes.Count; i++)
    {
    node.Nodes[i].CheckState
    = check;
    SetCheckedChildNodes(node.Nodes[i], check);
    }
    }

     


    /// 选择父节点时触发
    privatevoid SetCheckedParentNodes(TreeListNode node, CheckState check)
    {
      if (node.ParentNode !=null)
     {
        bool b =false;
        CheckState state;
        for (int i =0; i < node.ParentNode.Nodes.Count; i++)
       {
           state
    = (CheckState)node.ParentNode.Nodes[i].CheckState;
           if (!check.Equals(state))
           {
                  b
    =!b;
                  break;
           }
       }
       node.ParentNode.CheckState
    = b ? CheckState.Indeterminate : check;
       SetCheckedParentNodes(node.ParentNode, check);
     }
    }

    /// 判断此节点下的所有孩子节点是否选中
    private Boolean IsChildsChecked(TreeListNode node)
    {
      for (int i =0; i < node.Nodes.Count; i++)
       {
         if (node.Nodes[i].CheckState == CheckState.Unchecked)
              return     false;
           if (node.Nodes[i].HasChildren)
            IsChildsChecked(node.Nodes[i]);
         }
              return  true;
    }

     

    ////折叠当前节点及其所有子节点

     treeListPlate.CollapseAll();

    //获取焦点的节点

      TreeListNode node = treeListPlate.FocusedNode;

     


    /// 选中后高亮显示
    private void treeListPlate_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
    {
            TreeList node = sender as TreeList;
             if (e.Node == node.FocusedNode)
            {
               e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
               Rectangle r = new Rectangle(e.EditViewInfo.ContentRect.Left,
               e.EditViewInfo.ContentRect.Top,
               Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeListPlate.Font).Width + 1),
               Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeListPlate.Font).Height));

               e.Graphics.FillRectangle(SystemBrushes.Highlight, r);
               e.Graphics.DrawString(e.CellText, treeListPlate.Font, SystemBrushes.HighlightText, r);

               e.Handled = true;
            }
    }

     关于TreeList中节点的拖拽实现

    要实现TreeList控件中节点的拖拽首先将控件中的AllowDrop属性设置为True;

    并实现 DragEnter(在用鼠标将某项拖动到该控件的工作区域是发生) 与  DragDrop(拖放操作完成时发生)这2个拖拽事件;

           /// <summary>
            /// 拖拽的节点
            /// </summary>
            private TreeListNode dragNode;
            /// <summary>
            /// 原始父节点
            /// </summary>
            private TreeListNode orgParentNode;
            /// <summary>
            /// 当前父节点
            /// </summary>
            private TreeListNode pNode = null;
            private int index;
     /// <summary>
            /// 拖拽完成时发生
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void emTreeList_DragDrop(object sender, DragEventArgs e)
            {
                //鼠标单击节点时,获取节点信息
                TreeListHitInfo hitinfo = emTreeList.CalcHitInfo(emTreeList.PointToClient(new Point(e.X, e.Y)));
                TreeListNode node = hitinfo.Node;//当前点击的节点
                Console.WriteLine(node.GetValue("PUBLISHNAME"));
                Console.WriteLine("=="+index.ToString());
                
                try
                {
                    if (null != node && null != dragNode)
                    {
                        if (index == 0) //当前节点的子节点拖拽
                        {
                            pNode = node;
                        }
                        else //同级别
                        {
                            pNode = node.ParentNode;
                        }
                       
                        //此处根据需求自己写逻辑
          
    
                        emTreeList.LockReloadNodes();
                    }
                }
                catch (Exception)
                {
                 MessageBox.Show("当前节点不被允许移动到当前位置!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
    
    
            private void emTreeList_DragEnter(object sender, DragEventArgs e)
            {
              
                dragNode = emTreeList.FocusedNode;
                orgParentNode = dragNode.ParentNode;
            }
    
            private void emTreeList_CalcNodeDragImageIndex(object sender, DevExpress.XtraTreeList.CalcNodeDragImageIndexEventArgs e)
            {
                index = e.ImageIndex;
        
            }
  • 相关阅读:
    Python Django开发遇到的坑(版本不匹配)
    Mysql安装与问题合集
    git branch -r查看不了远程所有分支
    angularJS使用$http请求下载excel表格
    遍历formData对象数据
    按需使用CryptoJS之AES加密(CFB)模式
    git之创建、删除分支
    git pull时报错:Access Denied (拒绝访问)
    angularJS监听数据变化
    Angular-ui-router入门
  • 原文地址:https://www.cnblogs.com/h20064528/p/2593663.html
Copyright © 2011-2022 走看看