本文实例讲述了C#搜索TreeView子节点,保留父节点的方法。分享给大家供大家参考。具体如下:
C# 搜索TreeView子节点保留父节点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | private void selectNodeByName( string name) { //重新生生成树, InitTvCatalog(); //删除节点 selectNode(tvCatalog.Nodes, name); } private void selectNode(TreeNodeCollection nodes, string name) { bool del = true ; for ( int i = nodes.Count - 1; i >= 0; i--) { TreeNode node = nodes[i]; del = true ; if (node.Nodes.Count != 0) { selectNode(node.Nodes, name); } if (node.Text.StartsWith(name, StringComparison.OrdinalIgnoreCase)) { del = false ; } else { del = true ; } if (del && node.Nodes.Count == 0) { node.Remove(); } } tvCatalog.ExpandAll(); } |
WPF搜索TreeView子节点保留父节点:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | private void selectNodeByName( string name) { //重新生生成树, method = new Common.PublicMethod(); method.TreeViewLoad(); //删除节点 selectNode(RegimeSetting.Common.StaticVariables.treeView.Items, name); method = null ; } private void selectNode(ItemCollection nodes, string name) { bool del = true ; for ( int i = nodes.Count - 1; i >= 0; i--) { TreeViewItem node = nodes[i] as TreeViewItem; del = true ; if (node.Items.Count != 0) { selectNode(node.Items, name); } if (node.Header.ToString().StartsWith(name, StringComparison.OrdinalIgnoreCase)) { del = false ; } else { del = true ; } if (del && node.Items.Count == 0) { if ((node.Parent as TreeViewItem) != null ) (node.Parent as TreeViewItem).Items.Remove(node); else (node.Parent as TreeView).Items.Remove(node); } } (RegimeSetting.Common.StaticVariables.treeView.Items[0] as TreeViewItem).IsExpanded = true ; } |
希望本文所述对大家的C#程序设计有所帮助。