【发布时间】:2015-08-17 17:32:07
【问题描述】:
我为我的 TabControl 编写了一个样式。在 TabControl 中,我有一个 TextBlock 和一个 Button。我希望为 TabItem.IsSelected 设置触发器,以便 TextBlock 中文本的字体颜色发生变化。我下面的代码不起作用:
<Style x:Key="_tabItemButtonStyle" BasedOn="{StaticResource MetroTabItem}" TargetType="{x:Type TabItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Name="_TabHeaderStackPanel" >
<TextBlock Text="{ Binding TabName }" Name="_TabHeaderText" Background="{ Binding TabBackColour }" FontSize="{ Binding TabFontSize }" >
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=TabItem}}"
Value="True">
<Setter Property="Foreground" Value="SteelBlue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
...
我怀疑是这段代码的问题:
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelected,
RelativeSource={RelativeSource AncestorType=TabItem}}"
Value="True">
<Setter Property="Foreground" Value="SteelBlue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
编辑:
我将标签项绑定到 ViewModel 集合。所以我的样式绑定如下:
<TabControl x:Name="_MainTabControl" Grid.Column="1" Grid.Row="1"
SelectedIndex="0"
ItemsSource="{Binding OpenTabs}"
ItemContainerStyle="{StaticResource _tabItemButtonStyle}" />
【问题讨论】:
-
只需将带有触发器的样式移到
DataTemplate之外,它就可以工作了。附言把它放在你的TabControl.Resources。 HTH
标签: wpf xaml triggers tabcontrol tabitem