权限管理涉及复选框多勾选。
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); } ////// 设置子节点的状态 /// /// /// 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); } } /// /// 设置父节点的状态 /// /// /// 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);