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.
You can wrap your TreeView with Expander element:
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: