Wpf 默认样式/控制模板中的文本块颜色

Wpf 默认样式/控制模板中的文本块颜色,wpf,xaml,Wpf,Xaml,我有一个默认的GroupBox样式,我希望将默认的标题字体颜色设置为蓝色。在样式的ControlTemplate中设置时,我似乎无法获得要应用的颜色。因为它只是使用ContentPresenter作为标题,并且没有前台属性,所以我在ContentPresenter上设置TextElement.Foreground(我还尝试了TextBlock.Foreground),据我所知,这应该使ContentPresenter的所有TextBlock子级的前台都为蓝色。但是,此设置无效 我还尝试向Cont

我有一个默认的GroupBox样式,我希望将默认的标题字体颜色设置为蓝色。在样式的ControlTemplate中设置时,我似乎无法获得要应用的颜色。因为它只是使用ContentPresenter作为标题,并且没有前台属性,所以我在ContentPresenter上设置TextElement.Foreground(我还尝试了TextBlock.Foreground),据我所知,这应该使ContentPresenter的所有TextBlock子级的前台都为蓝色。但是,此设置无效

我还尝试向ContentPresenter添加一个资源,以覆盖默认样式的TextBlock,给它一个蓝色的前景,这也没有帮助。如果我在GroupBox本身上设置了前台属性,那么每个文本块(包括GroupBox的标题和内容)都会将其颜色设置为该属性,但我只想更改标题的颜色。我不想给标题一个明确的HeaderTemplate,因为我希望用户能够在标题中放入他们想要的任何内容,我只希望该内容有一个蓝色的前景

<Style TargetType="{x:Type GroupBox}">
    <Setter Property="BorderBrush" Value="{DynamicResource TabItem.Selected.BorderBrush}"/>
    <Setter Property="BorderThickness" Value="2"/>
    <Setter Property="FontSize" Value="18"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupBox}">
                <Grid SnapsToDevicePixels="true">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="6"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="6"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="6"/>
                    </Grid.RowDefinitions>
                    <Border BorderBrush="Transparent" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.ColumnSpan="4" Grid.Column="0" CornerRadius="5" Grid.Row="1" Grid.RowSpan="3"/>
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Grid.ColumnSpan="4" CornerRadius="5" Grid.Row="1" Grid.RowSpan="3">
                        <Border.OpacityMask>
                            <MultiBinding ConverterParameter="7" Converter="{StaticResource BorderGapMaskConverter}">
                                <Binding ElementName="Header" Path="ActualWidth"/>
                                <Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
                                <Binding Path="ActualHeight" RelativeSource="{RelativeSource Self}"/>
                            </MultiBinding>
                        </Border.OpacityMask>
                    </Border>
                    <Border x:Name="Header" Grid.Column="1" Padding="3,1,3,0" Grid.Row="0" Grid.RowSpan="2">
                        <ContentPresenter TextElement.Foreground="Blue" ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    <ContentPresenter Grid.ColumnSpan="2" Grid.Column="1" Margin="{TemplateBinding Padding}" Grid.Row="2" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


您的样式实际起作用,并在标题中显示蓝色文本。你确定在实际使用GroupBox时没有覆盖XAML中的蓝色吗?嗯,很有趣。。。我没有覆盖实际GroupBox或其标题上的任何颜色。如果没有显式设置,我还希望GroupBox从其父容器继承前景,但它没有这样做。当我实现上面的样式时,我确实注释掉了
中的所有内容,因为我没有访问转换器的权限。除此之外,我完全复制了它。也许在XAML中有什么东西导致了问题。这不重要,也没有任何影响。我尝试将ContentPresenter替换为一个文本块,该文本块由一个包含TextElement.Foreground=“Blue”的网格包装,并正确地为文本块着色,因此它似乎与ContentPresenter有关。。。我没有为ContentPresenter设置任何样式。实际上,如果我将ContentPresenter的内容设置为文本块,它也可以工作。。。当ContentSource=“Header”出现问题时。当然,这就像我在显式地设置内容的颜色,但我看不出这是在哪里发生的。。。不过谢谢你的帮助,我想我已经有足够的信息来追踪它了。知道它在某些情况下确实有效是有用的,我想我完全错过了一些东西。