TreeView Search with HierarchicalDataTemplate

2 posts, 0 answers
  1. Koren
    Koren avatar
    68 posts
    Member since:
    Oct 2010

    Posted 19 Nov 2010 Link to this post

    After combining the code from multiple examples in the forum, I am really close to getting my search working for my TreeView.  However, as other people have found, it will only work if I expand my whole tree first.  What do I need to modify in my code to make this work correctly?  Obviously, if my users have to expand the whole tree first, the search option is pretty useless.

    thanks!
    Koren

            private void SearchList(string searchValue)
            {
                ControlTemplate tt = (ControlTemplate)baseFrameworkElement.FindResource("NavigationContentTemplate");
                ContentControl tc = (ContentControl)baseFrameworkElement.FindName("NavigationContent");
                _treeView = tt.FindName("NavigationList", tc) as RadTreeView;
               
                //_treeView.ExpandAll();
                //_treeView.CollapseAll();
                SearchTree(searchValue.ToLower(), _treeView);
            }
            private void SearchTree(string searchValue, ItemsControl item)
            {           
                for (int i = 0; i < item.Items.Count; i++)
                //foreach (var _dataItem in item.Items)
                {
                    //RadTreeViewItem _childItem = treeView.ContainerFromItemRecursive(_dataItem);
                    RadTreeViewItem _childItem = item.ItemContainerGenerator.ContainerFromIndex(i) as RadTreeViewItem;
                    if (_childItem != null)
                    {
                        //MessageBox.Show(_childItem.ToString());

                        if (_childItem.Item is Association)
                        {
                            Association _childObject = _childItem.Item as Association;
                            if (_childObject.AssociationName.ToLower().Contains(searchValue))
                            {
                                if (_childItem.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                                {
                                    _childItem.IsExpanded = true;
                                    _childItem.BringIntoView();
                                }
                            }

                        }
                        else if (_childItem.Item is Economist)
                        {
                            Economist _childObject = _childItem.Item as Economist;
                            if (_childObject.EconomistName.ToLower().Contains(searchValue))
                            {
                                if (_childItem.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                                {
                                    _childItem.IsExpanded = true;
                                    _childItem.BringIntoView();
                                }
                            }

                        }
                        else if (_childItem.Item is Farm)
                        {
                            Farm _childObject = _childItem.Item as Farm;
                            if (_childObject.FarmName.ToLower().Contains(searchValue) || _childObject.FarmNbr.Contains(searchValue))
                            {
                                if (_childItem.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
                                {
                                    _childItem.IsExpanded = true;
                                    //_childItem.IsSelected = true;
                                    _childItem.BringIntoView();
                                }
                            }

                        }
                        SearchTree(searchValue, _childItem);
                    }
                }
            }

  2. Viktor Tsvetkov
    Admin
    Viktor Tsvetkov avatar
    382 posts

    Posted 24 Nov 2010 Link to this post

    Hi Koren,

    In order to get this work you should expand all parent items up to the root element like here, or you should use GetItemByPath method, as it is done here.

    Sincerely yours,
    Viktor Tsvetkov
    the Telerik team
    Browse the videos here>> to help you get started with RadControls for WPF
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top