public void SetRootNode(MyTreeNode rootNode) { pager.Visibility = Visibility.Hidden; memberTreeView.Items.Clear(); ringNodeIds.Clear(); if (rootNode != null) { TreeViewItem rootItem = NewTreeViewItem(rootNode); memberTreeView.Items.Add(rootItem); //如果还有子节点,则添加一个节点,使该节点具有折叠的"+" if (rootNode.ChildrenCount > 0) { rootItem.Items.Add(NewTreeViewItem(null)); } MyTrees.OpenDB(); //判断当前根节点是否存在父节点 if (MyTrees.GetNodeBySysId(rootNode.TopId) != null) { btnUpLevelNode.IsEnabled = true; btnUpRootNode.IsEnabled = true; } else { btnUpLevelNode.IsEnabled = false; btnUpRootNode.IsEnabled = false; } MyTrees.CloseDB(); } }
//显示到顶级根节点 private void btnUpRootNode_Click(object sender, RoutedEventArgs e) { TreeViewItem oldRootItem = memberTreeView.Items[0] as TreeViewItem; MyTreeNode oldRootNode = oldRootItem.Tag as MyTreeNode; MyTreeNode newRootNode = MyTrees.GetNodeBySysId(oldRootNode.TopId); if (isRingClose(newRootNode.SysId)) { return; } List <MyTreeNode> parentNodes = MyTrees.FindToRootNodeList(oldRootNode.TopId); if (parentNodes.Count > 0) { //先移除旧的根节点 memberTreeView.Items.Remove(oldRootItem); for (int i = 0; i < parentNodes.Count; i++) { MyTreeNode node = parentNodes[i]; //添加新的根节点 TreeViewItem newRootItem = NewTreeViewItem(parentNodes[i]); newRootItem.IsExpanded = true; newRootItem.Items.Add(oldRootItem); oldRootItem = newRootItem; } } memberTreeView.Items.Add(oldRootItem); btnUpLevelNode.IsEnabled = false; btnUpRootNode.IsEnabled = false; }
//显示上一级节点 private void btnUpLevelNode_Click(object sender, RoutedEventArgs e) { TreeViewItem oldRootItem = memberTreeView.Items[0] as TreeViewItem; MyTreeNode oldRootNode = oldRootItem.Tag as MyTreeNode; MyTreeNode newRootNode = MyTrees.GetNodeBySysId(oldRootNode.TopId); if (isRingClose(newRootNode.SysId)) { return; } //先移除旧的根节点 memberTreeView.Items.Remove(oldRootItem); //添加新的根节点 TreeViewItem newRootItem = NewTreeViewItem(newRootNode); memberTreeView.Items.Add(newRootItem); newRootItem.IsExpanded = true; //新的根节点添加子节点 List <MyTreeNode> childrenNodes = MyTrees.GetNodes_ByTopId(newRootNode.SysId); bool hasNotAdded = true; foreach (MyTreeNode subNode in childrenNodes) { if (hasNotAdded) { if (oldRootNode.SysId == subNode.SysId) { newRootItem.Items.Add(oldRootItem); hasNotAdded = false; continue; } } TreeViewItem subItem = NewTreeViewItem(subNode); newRootItem.Items.Add(subItem); //如果子节点还有孙子节点,则添加一个虚假的孙节点,使该子节点具有折叠的"+" if (subNode.ChildrenCount > 0) { subItem.Items.Add(NewTreeViewItem(null)); } } //判断当前根节点是否存在父节点 if (MyTrees.GetNodeBySysId(newRootNode.TopId) != null) { btnUpLevelNode.IsEnabled = true; btnUpRootNode.IsEnabled = true; } else { btnUpLevelNode.IsEnabled = false; btnUpRootNode.IsEnabled = false; } }