I'm pretty fresh to WPF and this is the closest I have come to achieving what I set out to do after reviewing many of the previously asked questions posted here. The XAML code:
<TreeView x:Name="folderView" Grid.Column="0" Grid.Row="1" BorderThickness="0">
<TreeViewItem Header="Folders" ItemsSource="{Binding SubFolders, Source={StaticResource RootFolderDataProvider}}" Margin="5"/>
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type my:FolderView}" ItemsSource="{Binding SubFolders}">
<StackPanel Orientation="Horizontal" Name="myPanel">
<Image x:Name="img" Width="16" Height="16" Source="Images/FolderClosed.png" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
<HierarchicalDataTemplate.Triggers>
<DataTrigger Binding="{Binding IsExpanded}" Value="True">
<Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
This displays the FolderClosed image on all my subfolders except the very top root folder.
The HierachicalDataTemplate
trigger also fails to fire when expanded.
Any help would be appreciated.
If you are binding to the TreeViewItem IsExpanded
property, then you will have to update your binding like:
<DataTrigger Binding="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="True">
<Setter TargetName="img" Property="Source" Value="Images/FolderOpen.png"/>
</DataTrigger>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With