Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:55:14+00:00 2026-05-26T10:55:14+00:00

I have the need to create a HierarchicalDataTemplate for a TreeView in code-behind. This

  • 0

I have the need to create a HierarchicalDataTemplate for a TreeView in code-behind.

This is what my XAML looks like:

<DataTemplate x:Key="DetailTemplate">
        <StackPanel Orientation="Horizontal">
            <Image Height="15" Width="15" Source="{Binding Image}" Margin="0,0,5,0"/>
            <TextBlock Text="{Binding Text}" />
        </StackPanel>
    </DataTemplate>

    <HierarchicalDataTemplate x:Key="MasterDetailTemplate" 
                              ItemsSource="{Binding SomeSource}" 
                              ItemTemplate="{StaticResource DetailTemplate}">
        <StackPanel Orientation="Horizontal">
            <Image Height="15" Width="15" Source="{Binding Image}" Margin="0,0,5,0"/>
            <TextBlock Text="{Binding Text}" />
        </StackPanel>
    </HierarchicalDataTemplate>

This is what i got so far in c#:

        Image image = new Image();
        image.Name = "image";
        image.Height = 15;
        image.Width = 15;

        Binding imageBinding = new Binding("Image");
        BindingOperations.SetBinding(image, Image.SourceProperty, imageBinding);

        TextBlock textBlock = new TextBlock();
        textBlock.Name = "textBlock";

        Binding textBinding = new Binding("Text");
        BindingOperations.SetBinding(textBlock, TextBlock.TextProperty, textBinding);

        StackPanel stackPanel = new StackPanel();
        stackPanel.Orientation = Orientation.Horizontal;

        stackPanel.Children.Add(image);
        stackPanel.Children.Add(textBlock);

        DataTemplate dataTemplate = new DataTemplate();
        dataTemplate.DataTemplateKey

I am stuck at the DataTemplateKey.

  • Is this possible to do in code behind?
  • Where do i go from here to set the x:Key value?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:55:15+00:00Added an answer on May 26, 2026 at 10:55 am

    OK In my comment to your question I specified the code behind way of specifying templates. Now to use / refer them with a key when we add them into ResourceDictionaties, we must add them with a Key.

       myWindow.Resources.Add("MasterDetailTemplate", dataTemplate);
    

    Instead of myWindow it can be myParentPanel i.e. any ancestor of your tree view.

    But there is one issue..

    The Key (i.e. the DataTemplate) doesnt exist at design time. You are creating and adding it at runtime.

    So if you are referring this datatemplate then

    1. Either refer the resource after it is are added to the resource dictionary.

      e.g.

      myWindow.Resources.Add(
           "MasterDetailTemplate",
           dataTemplate);
      
       myTreeView.ItemTemplate
         = myWindow.Resources["MasterDetailTemplate"] as HierarchicalDataTemplate;
      
    2. Refer the dynamically created data template as DynamicResource in XAML. DynamicResource removes the need of pre-existence of MasterDetailTemplate in any resource dictionary.

      <TreeView ItemTemplate="{DynamicResource MasterDetailTemplate}" ... >
        ....
      </TreeView>
      

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a need to create a private key that is seeded from 2
I have a need to create a library of Object Oriented PHP code that
I have the need to create Sharepoint blog comments by code: SPSecurity.RunWithElevatedPrivileges(delegate() { sw.AllowUnsafeUpdates
This is my first time I need to create a cutscene system. I have
I am learning JQuery. I have a need to create a custom control. This
This might be a really ignorant question, but I have a need to create
I have a need to create a transactional process using an external API that
I have the need to create the functionality to allow my registered users to
I have a need to create a HttpSession (via cookie) whenever a client invokes
I have a need to create a small pop up information box when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.