码迷,mamicode.com
首页 > 其他好文 > 详细

DevExpress XtraTreeList TreeList复选框选择

时间:2018-05-23 20:46:00      阅读:487      评论:0      收藏:0      [点我收藏+]

标签:treelist   tor   子节点   override   parent   pre   send   prime   tno   

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

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

DevExpress XtraTreeList TreeList复选框选择

标签:treelist   tor   子节点   override   parent   pre   send   prime   tno   

原文地址:https://www.cnblogs.com/TBW-Superhero/p/9078628.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!