zoukankan      html  css  js  c++  java
  • DevExpress XtraTreeList TreeList复选框选择

      权限管理涉及复选框多勾选。

    1.控件属性设置

    TreeList.OperationView.ShowCheckBoxes=true;用于显示CheckBox;

    TreeList.OperationBehavior.AllowIndeterminateCheckState=true;  设置CheckBox允许第三种状态。

    2.控件事件绑定

    要实现选择父级节点选择。子级节点全部选中。父级节点未选择。反之。子级节点部分选中。父级节点为第三种状态。

    private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
            {
                SetCheckedChildNodes(e.Node, e.Node.CheckState);
                SetCheckedParentNodes(e.Node, e.Node.CheckState);
    
            }
    
            private void treeList1_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
            {
                e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
            }
    
            /// <summary>
            /// 设置子节点的状态
            /// </summary>
            /// <param name="node"></param>
            /// <param name="check"></param>
            private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
            {
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    node.Nodes[i].CheckState = check;
                    SetCheckedChildNodes(node.Nodes[i], check);
                }
            }
    
            /// <summary>
            /// 设置父节点的状态
            /// </summary>
            /// <param name="node"></param>
            /// <param name="check"></param>
            private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.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);
                }
            }

    实现TreeList节点筛选及其节点定位

    1.定义类实现方法

     class FilterNodeOperation : TreeListOperation
        {
            string pattern;
    
    
            public FilterNodeOperation(string _pattern)
            {
                pattern = _pattern;
            }
    
    
            public override void Execute(TreeListNode node)
            {
                if (NodeContainsPattern(node, pattern))
                {
                    node.Visible = true;
    
                    //if (node.ParentNode != null)  
                    //    node.ParentNode.Visible = true;  
    
                    //必须要递归查找其父节点全部设置为可见  
                    var pNode = node.ParentNode;
                    while (pNode != null)
                    {
                        pNode.Visible = true;
                        pNode = pNode.ParentNode;
                    }
                }
                else
                    node.Visible = false;
            }
    
    
            bool NodeContainsPattern(TreeListNode node, string pattern)
            {
                foreach (TreeListColumn col in node.TreeList.VisibleColumns)
                {
                    if (node.GetDisplayText(col).Contains(pattern))
                        return true;
                }
                return false;
            }
        }

    2.调用方法

       var operation = new FilterNodeOperation(txtRightName.Text.Trim());
       treePriMenus.NodesIterator.DoOperation(operation);

    注:在初次加载权限列表时。将Node.Check=true;不会触发节点选中事件。因此不会存在第三种状态出现。需再次调用SetCheckedParentNodes方法

     if (node != null)
         node.CheckState =CheckState.Checked; 
         SetCheckedParentNodes(node, node.CheckState);
  • 相关阅读:
    Spring(7)AOP的相关概念(二)
    Spring(6)AOP的相关概念(一)
    Spring(5)基于注解的IOC配置
    Spring(4)使用 Spring的IoC的实现增删该查
    Spring(3)使用 spring 的IOC解决程序耦合
    Oracle体系结构概述(一)
    Spring(2) IoC 的概念和作用
    Spring(1)Spring概述
    Mybaits(15)注解开发
    Mybaits(14)存储过程
  • 原文地址:https://www.cnblogs.com/TBW-Superhero/p/9078628.html
Copyright © 2011-2022 走看看