我在使用触发器的ControlTemplate中设置前景色时遇到问题。
<LinearGradientBrush x:Key="TabItemDefaultBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
<GradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0.0" Color="#FFF" />
<GradientStop Offset="1.0" Color="#EEE" />
</GradientStopCollection>
</GradientBrush.GradientStops>
</LinearGradientBrush>
<SolidColorBrush x:Key="SolidBorderBrush" Color="#888" />
<SolidColorBrush x:Key="TabItemSelectBackgroundBrush" Color="#69C" />
<SolidColorBrush x:Key="PressedBrush" Color="#79C" />
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border
Name="tabBorder"
MinWidth="150"
MinHeight="50"
Margin="0,0,20,0"
Background="{StaticResource TabItemDefaultBackgroundBrush}"
BorderBrush="{StaticResource SolidBorderBrush}"
BorderThickness="1"
CornerRadius="1,1,1,1">
<Grid>
<Grid >
<TextBlock Name="HeaderHeader">
</TextBlock>
</Grid>
<ContentPresenter
x:Name="ContentSite"
Margin="12,2,12,2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ContentSource="Header"
RecognizesAccessKey="True" />
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="HeaderHeader" Property="Foreground" Value="White" />
<Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
选择选项卡时,背景边框“tabBorder”设置正确,但文本块“HeaderHeader”的前景没有响应。
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="HeaderHeader" Property="Foreground" Value="White" />
<Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
</Trigger>
</ControlTemplate.Triggers>
当我移除
TargetName
如果将前景设置为白色(对于所有组件),它就可以工作了(但很明显,它也是所有内容的前景色
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Foreground" Value="White" />
<Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
</Trigger>
</ControlTemplate.Triggers>
更奇怪的是,当我把
目标名
回到原来的位置,但是将setter属性从前台更改为后台,它确实有效!然后,所选选项卡的背景设置为白色。
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="HeaderHeader" Property="Background" Value="White" />
<Setter TargetName="tabBorder" Property="Background" Value="{StaticResource TabItemSelectBackgroundBrush}" />
</Trigger>
</ControlTemplate.Triggers>
它究竟为什么不能满足我的需要(=为所选选项卡设置前景)?!