Search code examples
wpfxamltreeviewtreeviewitemhierarchicaldatatemplate

WPF: styling a HierarchicalDataTemplate


Here is a simple DataTemplate

<Grid.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Items}" DataType="{x:Type entities:Folder}" ItemContainerStyle="{StaticResource FileComponentItem}">
    <Grid ShowGridLines="False" HorizontalAlignment="Center">
        <TextBlock Grid.Column="0" Text="{Binding Type}" />
    </Grid>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate ...... />
</Grid.Resources>

this is applied to TreeView

<TreeView Grid.Row="1" BorderThickness="0" ItemsSource="{Binding}"  ItemContainerStyle="{StaticResource FlattenedTreeViewItem}"></TreeView>

Notice that a style FileComponentItem is applied to a template for type entities:Folder

It works, accept the style is only applied to the children of this node (Folder node). meaning, only the child nodes of Folder, will get the style (whatever items are expanded under Folder, while Folder itself remains not styled).

I would like to be able to control the style of items to which the template is applied, not the children


Solution

  • Look at http://blogs.msdn.com/b/mikehillberg/archive/2009/10/30/treeview-and-hierarchicaldatatemplate-step-by-step.aspx

    The explanation and corresponding sample should take care of your question.