这是搜索树视图控件的正确方法吗?

问题描述:

我有一个带有树视图和文本框的表单.当用户在文本框中输入内容时,该应用会尝试在树形视图中搜索与用户键入内容相匹配的项目.因此,如果树形视图具有频带列表并且用户键入"ZZ",则它将 找到匹配的第一个项目,可能是"ZZ Top".

Hi, I have a form with a treeview and a textbox.  As the user types into the textbox, the app tries to search the treeview for the item that matches what the user types in.  So, if the treeview has a list of bands and the user types "ZZ", it will find the first item that matches, probably "ZZ Top".

无论如何,我编写了代码,看起来似乎工作正常.我遇到的问题是我是否以正确的方式进行了递归.我已经编写了使用两种方法的算法,即SearchTree和SearchChildren. SearchTree搜索顶部 对项目进行分级,然后调用SearchChildren递归搜索层次结构. SearchTree和SearchChildren中的许多代码都是多余的,在我看来,我应该只能使用一种方法来编写此代码.

Anyway, I wrote the code and it appears to work just fine.  The problem I'm having is whether I'm doing the recursion the right way.  I've written the algorithm to use two methods, SearchTree and SearchChildren.  SearchTree searches the top level items and calls SearchChildren to recursively search down the hierarchy.  Much of the code in SearchTree and SearchChildren is redudent and it seems to me that I should be able to write this with only one method. 

但是,为了简化为一种方法,我需要从最上面的项目开始. AFAIK,树状视图没有最重要的内容.相反,它具有项目的集合.

But, in order to simplify it to one method, I need to start with the top most item.  AFAIK, the treeview doesn't have a top item.  Instead, it has a collection of items.

有没有更好的方法来写这个?

Is there a better way to write this?

    private void txtSearch_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
    {
      SearchTree(txtSearch.Text, TreeView1);
    }
    private void SearchTree(string searchString, TreeView treeView)
    {
      TreeViewItem foundItem = null;
      foreach (TreeViewItem item in treeView.Items)
      {
        if (item.HasItems)
        {
          SearchChildren(searchString, item, ref foundItem);
        }
        else
        {
          bool isMatch = IsMatch(searchString, item);
          if (isMatch)
          {
            foundItem = item;
          }
        }
        if (foundItem != null)
        {
          break;
        }
      }
      if (foundItem != null)
      {
        ExpandPath(foundItem);
        foundItem.IsSelected = true;
      }
      else
      {
        //TODO: Clear selected items in treeView
      }
    }
    private void SearchChildren(string searchString, TreeViewItem searchedItem, ref TreeViewItem foundItem)
    {
      foreach (TreeViewItem item in searchedItem.Items)
      {
        if (item.HasItems)
        {
          SearchChildren(searchString, item, ref foundItem);
        }
        else
        {
          System.Diagnostics.Debug.WriteLine(item.Header.ToString());
          bool isMatch = IsMatch(searchString, item);
          if (isMatch)
          {
            foundItem = item;
          }
        }
        if (foundItem != null)
        {
          break;
        }
      }
    }
    private static bool IsMatch(string searchString, TreeViewItem searchedItem)
    {
      bool isMatch = false;
      string headerText = searchedItem.Header.ToString();
      if (string.Compare(searchString, 0, headerText, 0, searchString.Length, true) == 0)
      {
        isMatch = true;
      }
      return isMatch;
    }
    private static void ExpandPath(TreeViewItem item)
    {
      item.IsExpanded = true;
      if (item.Parent != null && item.Parent is TreeViewItem)
      {
        ExpandPath(item.Parent as TreeViewItem);
      }
    }

I-DotNet,

I-DotNet,

我希望您不要因为TreeView内置了TextSearch而浪费时间.请参阅

I hope you have not wasted your time since TreeView has TextSearch built in. See http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/4b79bc2b-bd66-4dbb-ae8a-2d9255ccfce8

希望这会有所帮助.