我在 WPF 中使用 MahApps Metro Styling 并尝试向 TreeViewItem 添加样式,以便节点旁边有一个文件夹图标。( http://mahapps.com/MahApps.Metro/ )
这相当简单,我所要做的就是覆盖 TreeViewItem 标题模板并添加到图像中。
问题是由于某种原因,新模板未应用于树中的第一个条目,正如您所看到的那样:
所有其他树节点工作正常,但第一个拒绝应用样式。
我已经确认这与 Controls.TreeView.xaml 中包含的 MahApps TreeView 样式有关(一个没有 MahApps 但具有与预期相同的自定义样式的新项目),但我无法确切看到发生了什么。
有没有人在使用 MahApps 时有任何造型 TreeViewItems 的经验?
<TreeView Grid.Column="0" Name="FolderView">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource MetroTreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="img"
Width="16"
Height="16"
Stretch="Fill"
Source="Images/Folder.png"
Margin="3"
VerticalAlignment="Center"
/>
<TextBlock Text="{Binding}" Margin="0,0" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>