/// <summary> /// 递归遍历节点 /// </summary> private void TravellNodes(HhMenuTreeNode currentNode, Action <HhMenuTreeNode> action) { action?.Invoke(currentNode); foreach (HhMenuTreeNode node in currentNode.Nodes) { //action?.Invoke(node); TravellNodes(node, action); } }
/// <summary> /// 递归查找指定菜单节点 /// </summary> /// <param name="menuId"></param> private HhMenuTreeNode FindNodeInternal(HhMenuTreeNode currentNode, string menuId) { foreach (HhMenuTreeNode node in currentNode.Nodes) { if (node.Id == menuId) { return(node); } var nd = FindNodeInternal(node, menuId); if (nd != null) { return(nd); } } return(null); }
/// <summary> /// 递归加载所有子菜单节点 /// </summary> /// <param name="currentNode">当前节点</param> /// <param name="parentId">父菜单Id</param> /// <returns></returns> private void LoadTreeNodes(HhMenuTreeNode currentNode, string parentId) { if (SourceNodes == null) { return; } var nodes = SourceNodes.Where(n => n.ParentId == parentId).OrderBy(n => n.Sort); foreach (var node in nodes) { //node.BackColor = Color.MediumTurquoise; currentNode.Nodes.Add(node); LoadTreeNodes(node, node.Id); } ; }
/// <summary> /// 新建菜单视图 /// </summary> /// <returns></returns> public void BuildView() { // 根节点 _rootNode = new HhMenuTreeNode(RootNodeText); // // 加载所有子菜单节点 LoadTreeNodes(_rootNode, ""); //注意:假设根菜单Id为"" // // 清空并添加根节点 this.Nodes.Clear(); if (NeedRootNode) { this.Nodes.Add(_rootNode); } else { for (int i = 0; i < _rootNode.Nodes.Count; i++) { this.Nodes.Add(_rootNode.Nodes[i]); } } }