This is a migrated thread and some comments may be shown as answers.

TreeView Search with HierarchicalDataTemplate

1 Answer 119 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Koren
Top achievements
Rank 1
Koren asked on 19 Nov 2010, 10:11 PM
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);
                }
            }
        }

1 Answer, 1 is accepted

Sort by
0
Viktor Tsvetkov
Telerik team
answered on 24 Nov 2010, 04:22 PM
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
Tags
TreeView
Asked by
Koren
Top achievements
Rank 1
Answers by
Viktor Tsvetkov
Telerik team
Share this question
or