Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I need a function which loops through all TreeViewItems and give beak the TreeViewItem which got a specific Header...

For example:

GetSearchedTreeViewItemByHeader(string _header)
{
  var searchedNode = new TreeViewItem();
  foreach(var node in treeView.Items)
  {
   if(node.Header == _header)
   {
    searchedNode = node
   }
  }

  return searchedNode
}


this only works for the first Item in the treeView, does anybody know how i loop through all items(also all childitems...)? i got no idea i only heard from a recursive solution but i dont know how to do it...

thanks in advance!


EDIT:
I use WPF

What I have tried:

TreeViewItems.Items check, for loop, while loop
Posted
Updated 10-Jul-17 4:43am
v2
Comments
Richard Deeming 6-Jul-17 11:21am    
Are you looking for one matching node, or all matching nodes?
Member 12969975 7-Jul-17 2:59am    
one matching node

The recursion on that task is so common that even Microsoft has published a recursice code snippet.

tip: if you search a specific item, you use it as additional input parameter.
 
Share this answer
 
Here's a generic extension method to iterate all nodes in any hierarchy:
private static IEnumerable<T> SelectRecursiveIterator<T>(IEnumerable<T> source, Func<T, IEnumerable<T>> getChildren)
{
    var stack = new Stack<IEnumerator<T>>();

    try
    {
        stack.Push(source.GetEnumerator());
        while (stack.Count != 0)
        {
            IEnumerator<T> iter = stack.Peek();
            if (iter.MoveNext())
            {
                T current = iter.Current;
                yield return current;

                IEnumerable<T> children = getChildren(current);
                if (children != null) stack.Push(children.GetEnumerator());
            }
            else
            {
                iter.Dispose();
                stack.Pop();
            }
        }
    }
    finally
    {
        while (stack.Count != 0)
        {
            stack.Pop().Dispose();
        }
    }
}

public static IEnumerable<T> SelectRecursive<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> getChildren)
{
    if (source == null) throw new ArgumentNullException(nameof(source));
    if (getChildren == null) return source;
    return SelectRecursiveIterator(source, getChildren);
}

With that in place, you can iterate all the nodes in your tree, and return either all matching nodes:
static IEnumerable<TreeNode> FindAllNodesByHeader(TreeView tree, string header)
{
    return tree.Items.Cast<TreeViewItem>().SelectRecursive(node => node.Items.Cast<TreeViewItem>()).Where(node => node.Header == header);
}

or just the first matching node:
static TreeNode FindFirstNodeByHeader(TreeView tree, string header)
{
    return tree.Items.Cast<TreeViewItem>().SelectRecursive(node => node.Items.Cast<TreeViewItem>()).FirstOrDefault(node => node.Header == header);
}
 
Share this answer
 
v4
Comments
Member 12969975 7-Jul-17 2:58am    
i got a wpf treeview control, so i get error that Itemscontrol got no definition for SelectRecursiv...
Richard Deeming 7-Jul-17 6:32am    
You should have mentioned that you were using WPF in your question!

Try:
tree.Items.SelectRecursive(node => node.Items).FirstOrDefault(node => node.Header == header);
Member 12969975 10-Jul-17 9:20am    
i tried this, but the wpf treeview control doesn't got the selectrecursive method
Richard Deeming 10-Jul-17 9:34am    
Looks like the Items collection doesn't implement IEnumerable<T>.

Try adding .Cast<TreeViewItem>() to the property:
tree.Items.Cast<TreeViewItem>().SelectRecursive(node => node.Items.Cast<TreeViewItem>()).FirstOrDefault(node => node.Header == header);
Member 12969975 10-Jul-17 10:48am    
static TreeViewItem FindFirstNodeByHeader(TreeView tree, string header)
{
return tree.Items.Cast<treeviewitem>().SelectRecursive(node => node.Items.Cast<treeviewitem>()).FirstOrDefault(node => node.Header == header);
}
i tried it like this and i get error that TreeViewItem has no definition for SelectRecursive

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900