我对 WPF 非常陌生,这是我在查看了此处发布的许多先前提出的问题后最接近实现我的目标的方式。XAML 代码:
<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>
这将在我的所有子文件夹上显示 FolderClosed 图像,除了最顶层的根文件夹。触发器在HierachicalDataTemplate
展开时也无法触发。任何帮助,将不胜感激。