Recursive Treeview Binding

4 posts, 0 answers
  1. Eric
    Eric avatar
    57 posts
    Member since:
    Aug 2012

    Posted 15 May 2014 Link to this post

    I have a structure that I believe is not supported by Hierarchical Templates. If I'm wrong can someone show me how to bind this structure with templates.

    ObservableCollection<AssessmentViewModel>

    AssessmentViewModel.QuestionGroupViewModels<QuestionGroupViewModel>

    QuestionGroupViewModel.QuestionViewModels<QuestionViewModel>

    QuestionGroupViewModel.ChildQuestionGroupViewModels<QuestionGroupViewModel>

    So each QuestionGroupViewModel can have a list of QuestionViewModels OR a list of ChildQuestionGroupViewModels and so on and so forth into infinity.

    Is this possible with Hierarchical Templates or do I need to write code to do this.
  2. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 20 May 2014 Link to this post

    Hi Eric,

    I think the ItemTemplateSelector of the RadTreeView will be helpful for your scenario. Basically you can create your template selector class derived from the DataTemplateSelector and choose the templates that you need based on the items in the tree.

    For your convenience I prepared a sample project that demonstrates the use of template selector.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Eric
    Eric avatar
    57 posts
    Member since:
    Aug 2012

    Posted 30 May 2014 in reply to Martin Link to this post

    Thanks Martin for the detailed example. This will definitely work.

    I have one more question that I can't find an answer for in the Google's tubes.

    When I'm in the selector and I come across an item I do not want to show in the treeview, how can I just skip that item and have the treeview just ignore it. I tried returning null instead of a template but that just gives me an empty node.

    Basically my Assessment View Model has several paths to go down, the grouping tree, and another collection with every question not grouped, etc.
  5. Martin
    Admin
    Martin avatar
    1101 posts

    Posted 31 May 2014 Link to this post

    Hello Eric,

    Keep in mind that the RadTreeView will generate container for each item in its ItemsSource. The element in the ItemTemplate that it is applied with the selector will be wrapped in this container. This is why even if you set an empty template the treeview will allocate space for an item.

    If you want to hide items from the treeview I can suggest you two approaches. First you can create IsVisible property in your business objects model and bind it to the ItemVisibility property of the RadTreeViewItems.

    <telerik:BooleanToVisibilityConverter x:Key="boolToVisConverter" />
    <Style TargetType="telerik:RadTreeViewItem">
        <Setter Property="ItemVisibility" Value="{Binding IsVisible, Converter={StaticResource boolToVisConverter}}" />
    </Style>
    I updated the project from my last reply so that it demonstrates this approach.

    Another options could be to filter the collection with your items before pass it to the treeview ItemsSource. However, this wont be very efficient if you are changing the collection at runtime.

    Please give the project a try and let me know if it helps.

    Regards,
    Martin
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
Back to Top