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 9164343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:39:36+00:00 2026-06-17T14:39:36+00:00

I am having a treeview in my wpf application which dynamically binds data from

  • 0

I am having a treeview in my wpf application which dynamically binds data from my viewmodel.

<TreeView Grid.Column="0"
          Grid.Row="0"
          ItemsSource="{Binding Main}"
          HorizontalAlignment="Stretch"
          Name="treeView1"
          VerticalAlignment="Stretch"
          Height="auto"
          Width="auto"
          SelectedItemChanged="treeView1_SelectedItemChanged"
          HorizontalContentAlignment="Stretch"
          VerticalContentAlignment="Top"
          Margin="0,0,5,5">
  <TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
      <Setter Property="IsExpanded"
              Value="{Binding IsExpanded, Mode=TwoWay}" />
      <Setter Property="IsSelected"
              Value="{Binding IsSelected, Mode=TwoWay}" />
      <Setter Property="FontWeight"
              Value="Normal" />
      <Style.Triggers>
        <Trigger Property="IsSelected"
                 Value="True">
          <Setter Property="FontWeight"
                  Value="Bold" />
        </Trigger>
      </Style.Triggers>
    </Style>
  </TreeView.ItemContainerStyle>
  <TreeView.Resources>
    <HierarchicalDataTemplate DataType="{x:Type local:MViewModel}"
                              ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Mname}" />
      </StackPanel>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:FViewModel}"
                              ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding FName}" />
      </StackPanel>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate DataType="{x:Type local:CViewModel}"
                              ItemsSource="{Binding Children}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Com}" />
      </StackPanel>
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type local:LViewModel}">
      <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Lo}" />
      </StackPanel>
    </DataTemplate>
  </TreeView.Resources>
</TreeView>

I need to add the header to the whole treeview say “Family” whose childs are the above treeview elements.
How can i add header to the whole treeview.so that when i click that header it should display all Mname and so on.
Please help me with this issue.

Edit:

i have used

  <HierarchicalDataTemplate  DataType="{x:Type TextBlock}" ItemsSource="{Binding Children}">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Family" />
                        </StackPanel>
  </HierarchicalDataTemplate>

in my TreeviewResource. But still i can’t assign the parent for the tree.

Edit 2:

<TreeView>
   <TreeViewItem ItemsSource="{Binding Main}" Header="Family">
      <TreeViewItemContainerStyle>
      //..
      </TreeViewItemContainerStyle>
      <TreeView.Resources>
      //..
      </TreeView.Resources>
   </TreeViewItem>
</TreeView>

Its showing error.

But when i do like below,

<TreeView>
       <TreeViewItemContainerStyle>
      //..
      </TreeViewItemContainerStyle>
      <TreeView.Resources>
      //..
      </TreeView.Resources>
   <TreeViewItem ItemsSource="{Binding Main}" Header="Family"/>
</TreeView>

The root item has been set. But Only the first set of child are displayed. Childs of child elements are not displaying.

  • 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-06-17T14:39:37+00:00Added an answer on June 17, 2026 at 2:39 pm

    You can wrap your TreeView with Expander element:

    <Expander Header="Family">
        <TreeView>
            ...
        <TreeView>
    </Expander>
    

    But it is not clear from your question which element you want to use as a Header.

    Edit:

    So, if you need a root element for a tree, you can add it explicitly in xaml:

    <TreeView> <!--ItemsSource you should set on a TreeViewItem-->
        <TreeViewItem ItemsSource="{Binding Main}" Header="Family">
            <TreeViewItem.ItemContainerStyle>
                ...
            </TreeViewItem.ItemContainerStyle>
            <TreeViewItem.Resources>
                ...
            </TreeViewItem.Resources>         
        </TreeViewItem>
    <TreeView>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some serious trouble creating a WPF TreeView with an Object databinding.
I'm having about 200 icons ,which I wanted to display in a treeview on
Having WPF application with complex UI and want to write CodedUITest scripts for the
I am making a WPF program that contains a TreeView. I am having a
I am having the tree with all my task which gets all the data
I'm having some trouble with a databound TreeView in WPF, basically I want a
I am having a treeview on my main form I have my code from
I'm working with a treeView data Binding in MVC, and I'm getting the following
I am having problems events raised from my viewmodel sometimes showing the datacontext as
I have a TreeView implemented in WPF that is bound to some XML data

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.