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

  • Home
  • SEARCH
  • 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 3280410
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:42:04+00:00 2026-05-17T19:42:04+00:00

Apologies for the long post — Read a few threads about this, but still

  • 0

Apologies for the long post — Read a few threads about this, but still find it hard to implement. Basically, I have a collection of objects, defined as:

public class LibData
{
    public string Name { get; set; }
    ObservableCollection<LibObject> _list;
    public ObservableCollection<LibObject> List { get { return _list; } }

    public LibData(string name, LibDataType type)
    {
        this.Name = name;
        _list = new ObservableCollection<LibObject>();
    }
}

and the object:

public class LibObject
{
    public string Name { get; set; }

    public LibObject(string name)
    {
        this.Name = name;
    }
}

My main problem is in the XAML, and styling this TreeView. I need to have a specific style for a “root” item, and a specific style for a “leaf”. Thing is, that one item in the bound list is “Root->Leaf”, and another is “Root->Child->Leaf”.
I tried this:

<TreeView x:Name="myTree" ItemsSource="{x:Static local:myDataList}">
     <TreeView.ItemTemplate>
         <HierarchicalDataTemplate ItemsSource="{Binding Path=List}" >
             <Grid>
                 <StackPanel Orientation="Horizontal">
                      <TextBlock Text="{Binding Path=Name}" />
                      <CheckBox IsChecked="True" Content="HeaderCheckbox"/>
                 </StackPanel>
             </Grid>
         <HierarchicalDataTemplate.ItemTemplate >
              <DataTemplate>
                  <Grid>
            <StackPanel Orientation="Horizontal">
                <CheckBox IsChecked="True" Content="LeafCheckbox" />
                <TextBlock Text="{Binding Path=Name}"/>
            </StackPanel>
                  </Grid>
              </DataTemplate>
          </HierarchicalDataTemplate.ItemTemplate>
      </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>

This obviously works fine for the “Root->Leaf” item, but not for the “Root-Child-Leaf”.
The XAML implementation seems to be more of a “hard coded” solution, where I know that the item layout is always “Root->Leaf” – how do I make this dynamic?
Again, I have seen solutions to different levels (including using converters), but the problem i’m having is that I need specific styles to root and leaf and nothing for levels in between.
I’m wondering if I’m looking at this completely wrong …

  • 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-17T19:42:04+00:00Added an answer on May 17, 2026 at 7:42 pm

    Easy way to do this. Pull the DataTemplates out of the TreeView and put them in the resources section. Specify the DataType property for each DataTemplate, but do not include a key. The ItemTemplateSelector (a property of type DataTemplateSelector) on the TreeView will do the magic of using whichever DataTemplate suits the correct item type.

    Create a HierarchicalDataTemplate for types Root and Child, and a DataTemplate for type Leaf.

    Something like this:

    <Window.Resources>
        <ResourceDictionary>
    
            <DataTemplate DataType="{x:Type local:Leaf}">
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="True" Content="LeafCheckbox" />
                        <TextBlock Text="{Binding Path=SomeValue}"/>
                    </StackPanel>
                </Grid>
            </DataTemplate>
    
            <HierarchicalDataTemplate DataType="{x:Type local:Child}"
                                      ItemsSource="{Binding Children}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Child " />
                    <TextBlock Text="{Binding Path=SomeValue}" />
                </StackPanel>
            </HierarchicalDataTemplate>
    
            <HierarchicalDataTemplate DataType="{x:Type local:Root}"
                                      ItemsSource="{Binding Children}">
                <Grid>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Root " />
                        <TextBlock Text="{Binding Path=SomeValue}" />
                    </StackPanel>
                </Grid>
            </HierarchicalDataTemplate>
    
        </ResourceDictionary>
    </Window.Resources>
    
    <Grid>
        <TreeView x:Name="myTree" ItemsSource="{Binding}" />
    </Grid>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies for the long post, but I wonder if I could get some more
Apologies in advance for the long-winded question. I'm really a database programmer, but have
I apologize in advance for the long post... I used to be able to
Apologies if any of what I'm about to say makes no sense or overlooks
I am just getting started with IoC containers so apologies if this is a
Greetings, Apologies in advance that I have not researched this toughly enough to answer
I am just checking out F#, so apologies if this is a silly question,
Apologies if I get the terminology wrong. How do I define a constraint in
What's the difference between the following two pieces of HTML (apologies if there are
Good morning, Apologies for the newbie question. I'm just getting started with ASP.NET internationalization

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.