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

Filtering in TreeView

1 Answer 191 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Prash
Top achievements
Rank 1
Prash asked on 26 Feb 2009, 09:22 AM
hello,

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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Bobi
Telerik team
answered on 27 Feb 2009, 03:33 PM
Hi Prash,

Here is a simple example with RadtreeView and a TextBox.

1. Add the following code to the tree view:

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

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.

Greetings,
Boryana
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.
Tags
TreeView
Asked by
Prash
Top achievements
Rank 1
Answers by
Bobi
Telerik team
Share this question
or