WPF: styling a HierarchicalDataTemplate
-
08-02-2021 - |
Question
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
The explanation and corresponding sample should take care of your question.
OTHER TIPS
Since you applied the style to the ItemContainer
and as the name suggests it will be applied to the items it contains which in this case will be the child nodes of folder.
So, you need to apply this style on your listbox
or any control where this Hierarchichal data template gets applied.