简体   繁体   English

在C#树视图中展开特定节点的所有父节点

[英]Expand all parent nodes of a specific node in C# treeview

I'm creating a recursive find method in C# Treeview , and I want to expand all parent nodes of the fined node. 我正在C# Treeview创建一个递归find方法,并且我想扩展罚款节点的所有父节点。 This is my code: 这是我的代码:

private void RecursivFindNode(RadTreeNodeCollection nodes, string nodeName2Find)
{
   foreach (RadTreeNode node in nodes)
   {
      if (node.Text.Contains(nodeName2Find))
       {
           node.BackColor = Color.Yellow;
           NodeExpand(node);
       }
       RecursivFindNode(node.Nodes);
    }

}

private void NodeExpand(RadTreeNode nodeExpand)
{
   while (nodeExpand != null)
   {
       nodeExpand.Expand();
       nodeExpand = nodeExpand.Parent;
    }
}

But I'm getting this error: 但我收到此错误:

Collection was modified; enumeration operation may not execute.

I know I can't modify the item which is in a foreach loop . 我知道我无法修改foreach loop So how can I make it work? 那么我该如何运作呢?

So as @Vlad suggested, I changed my foreach to this: 因此,正如@Vlad所建议的,我将我的foreach更改为:

foreach (var node in nodes.OfType<RadTreeNode>().ToArray())

And now everything works fine. 现在一切正常。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM