Filtering in TreeView

2 posts, 1 answers
  1. Prash
    Prash avatar
    27 posts
    Member since:
    Jan 2009

    Posted 26 Feb 2009 Link to this post


    I am trying to implement a filtering functionality, wherein say I have a field in the TreeView Severity, and I want to Filter the treeView based on this property then I type the severity level in some textbox, say I type "a" then it should show me all the nodes whose severity starting with "a", and this should be handled on the keypress.

    Any code highlighting this will be helpful.

  2. Answer
    Bobi avatar
    513 posts

    Posted 27 Feb 2009 Link to this post

    Hi Prash,

    Here is a simple example with RadtreeView and a TextBox.

    1. Add the following code to the tree view:

                <navigation:RadTreeViewItem Header="item3" x:Name="item3">
                    <navigation:RadTreeViewItem Header="test4" />
                <navigation:RadTreeViewItem Header="item31" x:Name="item31">
                    <navigation:RadTreeViewItem Header="test5" x:Name="test5" />
                    <navigation:RadTreeViewItem Header="test5" x:Name="test6" />

    2. Add a TextBox with TextChanged event:

    <TextBox x:Name="text" TextChanged="text_TextChanged" Height="50" Width="50" />

    3. In the code-behind add the following:

    private void text_TextChanged(object sender, TextChangedEventArgs e)
            string searchValue = this.text.Text;
            for (int i = 0; i < this.tree.Items.Count; i++)
                RadTreeViewItem item= tree.ItemContainerGenerator.ContainerFromIndex(i) as RadTreeViewItem;
                if (item.ToString().Contains(searchValue))
                    if (tree.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
                        item.IsExpanded = true;

    Run the application and enter the number 3 in the TextBox. After that you will see that item31 and item3 are expanded.
    This is a simple example only demonstrating the principle. If you want to have real filtration you will have to add some more code.
    I hope that this will help you in developing your application.

    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top