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

How to filter tree view with HierarchicalDataTemplate?

3 Answers 322 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Roman
Top achievements
Rank 1
Roman asked on 06 Jul 2011, 10:56 AM
Hello,

I have a tree view bound to hierarchical data using HierarchicalDataTemplate. I need to have an ability to filter content of the tree view when user type text in a textbox like in your demo project. The problem is that the view model is used by several views so it is not possible to filted data in view model and I cannot follow the solution from demo project. Is there a way to filter data in a view (tree view) without touching data in the view model? For plain collection it is easy to use CollectionViewSource but I don't know how to apply it to hierarchical data used by the tree. The additional difficulty is that I need to display parent node if it has any children even if it doesn't match the filter itself, like in demo project. Any suggestions? Thank you in advance.

3 Answers, 1 is accepted

Sort by
0
Petar Mladenov
Telerik team
answered on 11 Jul 2011, 12:53 PM
Hi Roman,

Supposing that you want to do custom filtering and you don't want to change the ViewModels, I suggest you to do re-binding when you filter. For instance, you can dynamically change the ItemsSource of the RadTreeView by first making a copy of the initial data and then filtering it or using a Linq methods that return IEnumerable when applied to ObservableCollection (probably the type of your ItemsSource collection). Please let us know if this is suitable for you.

Best wishes,
Petar Mladenov
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

0
Roman
Top achievements
Rank 1
answered on 11 Jul 2011, 05:04 PM
Hello Petar,

It is not a problem to do as you've suggested for the root level, but what to do with hierarchical template? ItemsSource there is binded to the Children property of the item which will not be affected by a filter. The main my question is may I write something in HierarchicalDataTemplate so its ItemsSource will be filterable? I've tried to use converter to filter ItemsSource but as it is called at least one time (several times occasionally) for each item that comes to view while scrolling (tree is virtualized), such dynamic filtering degrades the performance greatly.
So the only solution I left I think is the solution I was tried to avoid. Create a specific view model for the tree that will have its own copy of the tree structure and will filter this copy.

0
Petar Mladenov
Telerik team
answered on 14 Jul 2011, 02:06 PM
Hi Roman,

We discussed this with my team and we think that making a copy of the original source collection, filtering it and rebinding it is the preferred way in your scenario.

Kind regards,
Petar Mladenov
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
TreeView
Asked by
Roman
Top achievements
Rank 1
Answers by
Petar Mladenov
Telerik team
Roman
Top achievements
Rank 1
Share this question
or