首页 > 编程 > C# > 正文

C#搜索TreeView子节点,保留父节点的方法

2020-01-24 01:27:59
字体:
来源:转载
供稿:网友

本文实例讲述了C#搜索TreeView子节点,保留父节点的方法。分享给大家供大家参考。具体如下:

C# 搜索TreeView子节点保留父节点:

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子节点保留父节点:

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#程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表