简体   繁体   English

在 WPF Treeview 中搜索特定的 TreeViewItem

[英]search WPF Treeview for specific TreeViewItem

I am working with a wpf TreeView .我正在使用 wpf TreeView I am trying to search through all of the items in the TreeView to find the desired TreeViewItem within it.我正在尝试搜索TreeView中的所有项目以在其中找到所需的TreeViewItem

my code:我的代码:

parent is a string of the header of the desired item I am searching for. parent是我正在搜索的所需项目的标题string

foreach (TreeViewItem item in treeView.Items)
{    
   if (item.Header.ToString().Contains(parent))
   {
       //Do Something to item that is found 
   }
}

The problem that I am having with this code is that it will search through the top two layers of the TreeView .我在这段代码中遇到的问题是它将搜索TreeView的前两层。 If there is a item within a item within the TreeView , then the item is not found.如果TreeView某个item中有一个item ,则找不到该item With that being said I added to my code but it only prolonged the issue another level into the TreeView .话虽如此,我添加到我的代码中,但它只会将问题延长到TreeView另一个级别。

foreach (TreeViewItem item in treeView.Items)
{
   foreach(TreeViewItem subItem in item.Items)
   {
       if (subItem.Header.ToString().Contains(parent))
       {
           //Do Something to item that is found         
       }
   }    
   if (item.Header.ToString().Contains(parent))
   {
       //Do Something to item that is found 
   }
}

Is there a way to search through the entire TreeView along with all the items within it?有没有办法搜索整个TreeView及其中的所有items Could I possible search the TreeView recursively?我可以递归搜索TreeView吗?

You should look into recursion.你应该研究递归。 Essentially, you want a method that takes a treeviewitem and iterates through all it's children.本质上,您需要一个方法来获取一个 treeviewitem 并遍历它的所有子项。 For each of those the method calls itself, passing in the child.对于这些方法中的每一个,方法都会调用自己,并传入子项。 If you've never done this before it's a bit mind melting when you first look at it.如果您以前从未这样做过,那么当您第一次看到它时,您会有点头脑发热。 Get an understanding of the principle.了解原理。 But roughly:但大致上:

    private void recurseItem(TreeViewItem item)
    {
        foreach (var subItem in item.Items)
        {
            // compare header and return that item if you find it
            if(!gotTheItem)
               recurseItem(subItem);
        }
    }

You need a foreach loop through the tree's top level items passing each into it.您需要一个 foreach 循环遍历树的顶级项目,将每个项目传递给它。 Don't just paste this in (you just learn cut and paste then) read up on recursion and think about it.不要只是粘贴它(然后你只是学习剪切和粘贴)阅读递归并考虑它。 Note that you will want to signal you found the item by setting a bool flag once you got it and avoid iterating all the rest.请注意,一旦获得该项目,您将希望通过设置一个 bool 标志来表明您已找到该项目,并避免迭代所有其余项目。

Note that this code is largely intended to introduce a newbie to the idea of recursion.请注意,这段代码主要是为了向新手介绍递归的概念。 It is not intended to be an ideal cut and paste solution.它不是理想的剪切和粘贴解决方案。
If you try this with a bound treeview then you will find that the Items collection has the objects you bound rather than treeviewitems.如果您使用绑定树视图尝试此操作,那么您会发现 Items 集合具有您绑定的对象而不是树视图项。

Andy's solution will work for your needs, but a more versatile and reusable solution would be to iterate using ItemCollection . Andy 的解决方案将满足您的需求,但更通用和可重用的解决方案是使用ItemCollection进行迭代。 With this you can pass any object type that inherits an ItemCollection while avoiding exceptions for sub-items that don't.有了这个,您可以传递任何继承ItemCollection对象类型,同时避免不继承的子项的异常。 This a great if you have different types of children in your tree.如果您的树中有不同类型的孩子,这很好。

private void IterateTree(ItemCollection items)
{     
    //Iterate each item
    foreach (var item in items)
    {
        //Your IF logic here

        //Iterate again if item contains sub items.
        if (item is ItemsControl)
        {
            ItemsControl ic = (ItemsControl)item;
            IterateTree(ic.Items); //Recursive call
        }
    }      
 }

I made two recursive methods.我做了两个递归方法。 the first initially searched the TreeView for the desired item, then the second searched each item until the desired item was found.第一个最初在TreeView搜索所需的项目,然后第二个搜索每个item直到找到所需的项目。 the method then returned the desired item which I was able to manipulate like I wanted to.然后该方法返回所需的项目,我可以按照自己的意愿操作。

Code:代码:

/// <summary>
    /// Recursivly search the treeview until desired TreeViewItem is found
    /// </summary>
    /// <param name="_treeView"></param>
    /// <param name="_parent"></param>
    /// <returns></returns>
    private TreeViewItem SearchTreeView(TreeView _treeView, string _parent)
    {
        TreeViewItem foundItem = null;
        foreach (TreeViewItem item in _treeView.Items)
        {
            if (item.Header.ToString().Contains(_parent))
            {
                foundItem = item;
            }
            else
            {
                foundItem = SearchTreeView(item, _parent);
            }
        }
        return foundItem;
    }
    /// <summary>
    /// Recursivly search the treeviewitem until desired TreeViewItem is found
    /// </summary>
    /// <param name="_item"></param>
    /// <param name="_parent"></param>
    /// <returns></returns>
    private TreeViewItem SearchTreeView(TreeViewItem _item, string _parent)
    {
        TreeViewItem foundItem = null;
        foreach (TreeViewItem subItem in _item.Items)
        {
            if (subItem.Header.ToString().Contains(_parent))
            {
                foundItem = subItem;
            }
            else
            {
                foundItem = SearchTreeView(subItem, _parent);
            }
        }
        return foundItem;
    }

using ItemsControl you can search in both TreeView and TreeViewItem subitems.使用ItemsControl您可以在TreeViewTreeViewItem子项中进行搜索。 here is my code:这是我的代码:

    private TreeViewItem SearchTreeView(ItemsControl item, string header)
    {
        TreeViewItem foundItem = null;
        foreach (TreeViewItem subItem in item.Items)
            foundItem = subItem.Header.ToString().Contains(header) ? subItem : SearchTreeView(subItem, header);
        return foundItem;
    }

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

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