在 WPF Treeview 中搜索特定的 TreeViewItem [英] search WPF Treeview for specific TreeViewItem

查看:76
本文介绍了在 WPF Treeview 中搜索特定的 TreeViewItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 wpf TreeView.我正在尝试搜索 TreeView 中的所有项目,以在其中找到所需的 TreeViewItem.

I am working with a wpf TreeView. I am trying to search through all of the items in the TreeView to find the desired TreeViewItem within it.

parent 是我正在搜索的所需项目的标题的string.

parent is a string of the header of the desired item I am searching for.

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

我在使用此代码时遇到的问题是它将搜索 TreeView 的前两层.如果TreeView 中的item 中有item,则找不到item.话虽如此,我添加到我的代码中,但它只会将问题延长到 TreeView 的另一个级别.

The problem that I am having with this code is that it will search through the top two layers of the TreeView. If there is a item within a item within the TreeView, then the item is not found. With that being said I added to my code but it only prolonged the issue another level into the 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 
   }
}

有没有办法搜索整个TreeView 以及其中的所有items?我可以递归搜索 TreeView 吗?

Is there a way to search through the entire TreeView along with all the items within it? Could I possible search the TreeView recursively?

推荐答案

你应该研究递归.本质上,您需要一个方法来获取一个 treeviewitem 并遍历它的所有子项.对于这些方法中的每一个,方法都会调用自己,并传入子项.如果您以前从未这样做过,那么当您第一次看到它时,您会有点头脑发热.了解原理.但大致上:

You should look into recursion. Essentially, you want a method that takes a treeviewitem and iterates through all it's children. 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);
        }
    }

您需要一个 foreach 循环遍历树的顶级项目,将每个项目传递给它.不要只是粘贴它(然后你只是学习剪切和粘贴)阅读递归并考虑它.请注意,您将希望在获得该项目后通过设置 bool 标志来表示您已找到该项目,并避免迭代所有其余项目.

You need a foreach loop through the tree's top level items passing each into it. 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.

请注意,这段代码主要是为了向新手介绍递归的概念.它不是理想的剪切和粘贴解决方案.
如果您使用绑定树视图尝试此操作,那么您会发现 Items 集合具有您绑定的对象而不是树视图项.

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.

这篇关于在 WPF Treeview 中搜索特定的 TreeViewItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆