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

Recursive Treeview Binding

3 Answers 131 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Eric
Top achievements
Rank 1
Eric asked on 15 May 2014, 02:05 PM
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.

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 20 May 2014, 10:58 AM
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.
 
0
Eric
Top achievements
Rank 1
answered on 30 May 2014, 07:08 PM
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.
0
Martin Ivanov
Telerik team
answered on 31 May 2014, 10:58 AM
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.
 
Tags
TreeView
Asked by
Eric
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Eric
Top achievements
Rank 1
Share this question
or