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

How to create a HierarchicalDataTemplate from code behind?

2 Answers 450 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sabuj
Top achievements
Rank 1
Sabuj asked on 02 Apr 2014, 02:12 PM


I have the following currently in my View xaml code. However here we have a fixed hierarchy in this case but want to create a custom hierarchy instead. therefore we can do that only if we can create the HierarchicalDataTemplate(defined below) programmatically. I have been unable to find reliable example thus far. IS this possible and if so could some one point me to any telerik documentation on how one can do this?

I apologize if this is not the correct location for posting this thread.

                  <DataTemplate x:Key="dtDocumentName">
                        <TextBlock Text="{Binding Path=Value.DocumentName}"/>
                    </DataTemplate>
                    <HierarchicalDataTemplate x:Key="hdtDocumentType" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource dtDocumentName}">
                        <TextBlock Text="{Binding Path=Name}" />
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate x:Key="hdtDocumentCategory" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource hdtDocumentType}">
                        <TextBlock Text="{Binding Path=Name}" />
                    </HierarchicalDataTemplate>
                    <HierarchicalDataTemplate x:Key="hdtDocumentRoot" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource hdtDocumentCategory}">
                        <TextBlock Text="{Binding Path=Name}" />
                    </HierarchicalDataTemplate>

2 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 07 Apr 2014, 11:03 AM
Hello Sabuj,

You could create the HierarchicalDataTemplate in code-behind without a problem, here are a fee examples on StackOverflow and MSDN that demonstrate the approach:
HierarchicalDataTemplate in code
How to create a HierarchicalDataTemplate in code-behind?

The provided template can be created with code like this:

HierarchicalDataTemplate hdt = new HierarchicalDataTemplate();
hdt.ItemsSource = new Binding("Items");
FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));
tb.SetBinding(TextBlock.TextProperty, new Binding("Name"));
hdt.VisualTree = tb;
           
TreeView1.ItemTemplate = hdt;

Hope this helps.

Regards,
Yana
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sabuj
Top achievements
Rank 1
answered on 07 Apr 2014, 01:42 PM
thanks Yana, this was what i was looking for. great help.
Tags
General Discussions
Asked by
Sabuj
Top achievements
Rank 1
Answers by
Yana
Telerik team
Sabuj
Top achievements
Rank 1
Share this question
or