How to filter tree view with HierarchicalDataTemplate?

4 posts, 0 answers
  1. Roman
    Roman avatar
    15 posts
    Member since:
    Jan 2011

    Posted 06 Jul 2011 Link to this post

    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.
  2. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 11 Jul 2011 Link to this post

    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!

  3. DevCraft banner
  4. Roman
    Roman avatar
    15 posts
    Member since:
    Jan 2011

    Posted 11 Jul 2011 Link to this post

    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.

  5. Petar Mladenov
    Admin
    Petar Mladenov avatar
    2891 posts

    Posted 14 Jul 2011 Link to this post

    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!

Back to Top