I need to add images to WPF treeview nodes, I've had a look at this example How do I add icons next to the nodes in a WPF TreeView? and it's working fine except ALL nodes have the same image.I would like all the nodes in the treeview which do not have any children to either have no image or have a different image.
Here is my XAML where I set the image:
<HierarchicalDataTemplate x:Key="NodeTemplate">
<StackPanel Orientation="Horizontal" Margin="2">
<Image Source="test.png" Width="16" Height="16" SnapsToDevicePixels="True"/>
<TextBlock x:Name="tb"/>
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<Binding>
<Binding.XPath>child::node()</Binding.XPath>
</Binding>
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
Below is a screen shot of the output
Could someone please suggest how can I achieve this, the possible solution can be either changing the XAML or programatically via C#.
Here is some code I used to solve almost the same problem. (The data I designed this for is XML data, so XPath="@name" means the value of the attribute name of the node, while Name means element type.)
Converter: