I have a TreeView component, and it can contain several types of nodes. Its all working well except I have multiple hierarchical data templates which differ just in the glyph image they display, DataType and ItemsSource but everything else is exactly the same.
I was wondering, is it possible to create just one ControlTemplate for all hierarchical data template and just modify the icon image by using the tag property?
So for example I have basic TreeNode class and nodes like, PersonNode, PropertyNode, StreetNode which inherit from TreeNode. TreeNode displays a folder icon, PersonNode displays a user icon, PropertyNode displays house icon and StreeNode displays street icon. So, actually they all have same content structure except, the icon, DataType and ItemsSource are changing.
How can I simplify my hierarchical data templates?
Sample code:
<HierarchicalDataTemplate
DataType="{x:Type MyTreeFolder:TreeNode}"
ItemsSource="{Binding Path=Items}">
<StackPanel Orientation="Horizontal">
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSpecialNode}" Value="True">
<Setter Property="Background" Value="Blue"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
<glyphs:GlyphAwesome
FontSize="12"
Glyph="folder"
Margin="0, 0, 4, 0"
VerticalAlignment="Center"/>
<TextBlock Text="{Binding}" VerticalAlignment="Center"/>
</StackPanel>
</HierarchicalDataTemplate>