顺便说一句,几天前我做了类似的事情。在我的应用程序中,在 HierarchicalDataTemplate 中将文件夹图标添加到那些行为类似于文件夹的对象中,我使用触发器根据项目是否展开来更改图标,以下是 XAML 的相关部分:
<HierarchicalDataTemplate DataType="{x:Type data:FeedComposite}"
ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal" Margin="1">
<StackPanel.Children>
<Image>
<Image.Style>
<Style BasedOn="{StaticResource IconImageStyleSmall}" TargetType="Image">
<Setter Property="Source" Value="{Binding Source={StaticResource Icon_FolderClosed}, Mode=OneTime}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=TreeViewItem}, Path=IsExpanded}" Value="True">
<Setter Property="Source" Value="{Binding Source={StaticResource Icon_FolderOpen}, Mode=OneTime}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<TextBlock Text="{Binding Title}"/>
</StackPanel.Children>
</StackPanel>
</HierarchicalDataTemplate>
Where {StaticResource Icon_FolderOpen}
and {StaticResource Icon_FolderClosed}
are BitmapImages
包含文件夹状态的图标。IconImageStyleSmall
是一种设置的样式MaxWidth
and MaxHeight
图像到适当的东西。
Edit:为了完成的缘故。
<BitmapImage x:Key="Icon_FolderOpen" UriSource="pack://application:,,,/ImageResources/Icons/FolderOpen.ico" />
<BitmapImage x:Key="Icon_FolderClosed" UriSource="pack://application:,,,/ImageResources/Icons/FolderClosed.ico" />
<Style x:Key="IconImageStyleSmall" TargetType="Image">
<Setter Property="MaxWidth" Value="16"/>
<Setter Property="MaxHeight" Value="16"/>
<Setter Property="Margin" Value="1"/>
</Style>
使用的图标 https://stackoverflow.com/questions/4142203/does-net-have-icon-collections/4142304#4142304