如何使用ControlTemplate中的触发器覆盖常规样式?

时间:2017-08-07 19:47:09

标签: wpf xaml tabcontrol controltemplate

我有一个TabControl,显示选定选项卡的不同Tab标题前景和背景。但我想在选项卡项内容控件中为TextBlocks设置一般前景色。所发生的事情是所有标题都获得了一般的TextBlock前景色,并且当选择了标签时,标签控件没有改变标题的前景色。

所以在我的主窗口中我有:

<Grid>
    <TabControl Style="{StaticResource TabControlStyle}">
        <TabItem Header="Tab 1" IsSelected="True">
            <TextBlock Text="Text in Tab 1"/>
        </TabItem>
        <TabItem Header="Tab 2">
            <TextBlock Text="Text in Tab 2"/>
        </TabItem>
    </TabControl>
</Grid>

在我的资源文件中,我已经定义了颜色和内容模板:

<Color x:Key="DarkGray">#404040</Color>
<Color x:Key="DarkGreen">#3A5038</Color>
<Color x:Key="ForegroundColor">#FFF1F1F1</Color>

<SolidColorBrush x:Key="DarkGrayBrush"
                 Color="{StaticResource DarkGray}" />
<SolidColorBrush x:Key="ForegroundColorBrush"
                 Color="{StaticResource ForegroundColor}" />
<SolidColorBrush x:Key="DarkGreenBrush"
                 Color="{StaticResource DarkGreen}" />

<Style TargetType="TextBlock">
    <Setter Property="Foreground"
            Value="{StaticResource DarkGrayBrush}" />
</Style>

<Style x:Key="TabControlStyle"
       TargetType="TabControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabControl">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <TabPanel Grid.Row="0"
                              Background="{TemplateBinding Background}"
                              IsItemsHost="true" />
                    <ContentPresenter Grid.Row="1"
                                      ContentSource="SelectedContent" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Resources>
        <Style TargetType="TabItem">
            <Setter Property="BorderThickness"
                    Value="0" />
            <Setter Property="FontSize"
                    Value="16" />
            <Setter Property="HeaderTemplate">
                <Setter.Value>
                    <DataTemplate>
                        <Border>
                            <ContentPresenter Content="{TemplateBinding Content}" />
                        </Border>
                    </DataTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TabItem">
                        <Border Name="Border"
                                Width="145"
                                Margin="10"
                                Padding="0"
                                BorderThickness="0"
                                CornerRadius="20">
                            <ContentPresenter x:Name="ContentSite"
                                              Margin="10"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              ContentSource="Header" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                     Value="True">
                                <Setter TargetName="Border"
                                        Property="Background"
                                        Value="{StaticResource DarkGreenBrush}" />
                                <Setter Property="Foreground"
                                        Value="{StaticResource ForegroundColorBrush}" />
                            </Trigger>
                            <Trigger Property="IsSelected"
                                     Value="False">
                                <Setter Property="Foreground"
                                        Value="{StaticResource DarkGrayBrush}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Style.Resources>
</Style>

我的目标是在Windows 10操作系统上使用.NET 4.5。

谢谢,威尔

1 个答案:

答案 0 :(得分:0)

设置TextBlock.Foreground的{​​{1}}属性:

ContentPresenter