【发布时间】:2012-02-25 00:19:06
【问题描述】:
我正在尝试更改 TreeView 中的 SelectedItem 模板。 我在 Style.Triggers 中编写了简单的容器样式并更改了项目模板,如所述
[1]:How do I highlight a treeview selected item with some color? 或
[2]: WPF TreeView: How to style selected items with rounded corners like in Explorer 但它不起作用。
然后我新建了一个项目,用简单的样式和模板创建了TreeView
<TreeView>
<TreeViewItem Header="Item1" />
<TreeViewItem Header="Item2" />
<TreeViewItem Header="Item3"/>
<TreeView.Resources>
<DataTemplate DataType="{x:Type TreeViewItem}" x:Key="selectedTemplate">
<StackPanel Height="25">
<TextBlock Text="SelectedItem"/>
</StackPanel>
</DataTemplate>
</TreeView.Resources>
<TreeView.ItemContainerStyle>
<Style TargetType="TreeViewItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontStyle" Value="Italic"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Background" Value="Green"/>
<Setter Property="ItemTemplate" Value="{StaticResource selectedTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
所以,然后我在树形视图中选择了TreeViewItem FontWeight,FontStyle 和Foreground 改变了,但是Background 和ItemTemplate 没有改变。
结果:
你能解释一下这种奇怪的行为吗?
【问题讨论】:
标签: wpf triggers treeview itemtemplate