DevExpress XtraTreeList TreeList複選框選擇

  權限管理涉及複選框多勾選。node

1.控件屬性設置ide

TreeList.OperationView.ShowCheckBoxes=true;用於顯示CheckBox;spa

TreeList.OperationBehavior.AllowIndeterminateCheckState=true;  設置CheckBox容許第三種狀態。code

2.控件事件綁定blog

要實現選擇父級節點選擇。子級節點所有選中。父級節點未選擇。反之。子級節點部分選中。父級節點爲第三種狀態。遞歸

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.定義類實現方法string

 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.調用方法io

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

注:在初次加載權限列表時。將Node.Check=true;不會觸發節點選中事件。所以不會存在第三種狀態出現。需再次調用SetCheckedParentNodes方法class

 if (node != null)
     node.CheckState =CheckState.Checked; 
     SetCheckedParentNodes(node, node.CheckState);
相關文章
相關標籤/搜索