How to create a HierarchicalDataTemplate from code behind?

3 posts, 0 answers
  1. Sabuj
    Sabuj avatar
    3 posts
    Member since:
    Apr 2012

    Posted 02 Apr 2014 Link to this post

    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}"/>
                        <HierarchicalDataTemplate x:Key="hdtDocumentType" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource dtDocumentName}">
                            <TextBlock Text="{Binding Path=Name}" />
                        <HierarchicalDataTemplate x:Key="hdtDocumentCategory" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource hdtDocumentType}">
                            <TextBlock Text="{Binding Path=Name}" />
                        <HierarchicalDataTemplate x:Key="hdtDocumentRoot" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource hdtDocumentCategory}">
                            <TextBlock Text="{Binding Path=Name}" />
  2. Yana
    Yana avatar
    4547 posts

    Posted 07 Apr 2014 Link to this post

    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.


    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.

  3. Sabuj
    Sabuj avatar
    3 posts
    Member since:
    Apr 2012

    Posted 07 Apr 2014 in reply to Yana Link to this post

    thanks Yana, this was what i was looking for. great help.
Back to Top