默认样式的TextBlock颜色/ ControlTemplate

时间:2016-02-25 18:22:12

标签: wpf xaml

我有一个默认的GroupBox样式,我想在其中将默认标题字体颜色设置为蓝色。从样式中的ControlTemplate中设置时,我似乎无法应用颜色。因为它只是使用ContentPresenter作为标题并且没有Foreground属性,所以我在ContentPresenter上设置TextElement.Foreground(我也尝试过TextBlock.Foreground),据我所知它应该使所有的TextBlock子元素ContentPresenter的前景为蓝色。但设置此项无效。

我还尝试向ContentPresenter添加一个资源来覆盖TextBlock的默认样式,为它提供一个蓝色前景,这也没有帮助。如果我在GroupBox本身上设置Foreground属性,则包含标题和GroupBox内容的每个TextBlock都会将其颜色设置为该颜色,但我只想更改标题的颜色。我不想给标题一个明确的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>

0 个答案:

没有答案