Wpf – GroupBox header template

foregroundheaderstylestemplateswpf

My Groupbox template is defined as so

<Style x:Key="{x:Type GroupBox}" TargetType="{x:Type GroupBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupBox}">
                <Border BorderThickness="1" BorderBrush="SomeColour" 
                        Background="SomeColour"
                        CornerRadius="4">
                    <Grid SnapsToDevicePixels="true">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="auto" />
                            <RowDefinition Height="auto" />
                        </Grid.RowDefinitions>
                        <ContentPresenter Grid.Row="0" ContentSource="Header" 
                                          Margin="2"/>
                        <ContentPresenter Grid.Row="1"
                                          Margin="{TemplateBinding Padding}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

How do I make it so that if the Header is set to a simple simple string, e.g

<GroupBox Header="The header!" />

The text is bold and with some default colour?

I tried the following, but it only works for the weight, not the colour.

<ContentPresenter ContentSource="Header" TextBlock.Foreground="Red" 
                  TextBlock.FontWeight="Bold"/>

Edit : here is the textblock style

<Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="{StaticResource LabelForegroundBrush}" />
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="VerticalAlignment" Value="Center" />
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="1" />

        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="{StaticResource DisabledLabelForegroundBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>

Edit 2 : If I place the following in <Window.Resources> it seems to work, yet if I place them in <Application.Resources>, .. it fails???

<XXX.Resources>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground" Value="Green" />
        </Style>

        <Style x:Key="{x:Type GroupBox}" TargetType="{x:Type GroupBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type GroupBox}">
                        <Grid SnapsToDevicePixels="true">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <ContentPresenter Grid.Row="0" ContentSource="Header" TextElement.Foreground="Red" />
                            <ContentPresenter Grid.Row="1" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </XXX.Resources>

Usage :

<GroupBox Header="Header">
        <Button Content="Content" />
    </GroupBox>

Best Answer

Try this

 <Style x:Key="{x:Type GroupBox}" TargetType="{x:Type GroupBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupBox}">
                    <Border BorderThickness="1" BorderBrush="SomeColour" 
                    Background="SomeColour"
                    CornerRadius="4">
                        <Grid SnapsToDevicePixels="true">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>
                            <TextBlock Grid.Row="0" Foreground="Red" FontWeight="Bold" Margin="2"> 
                            <ContentPresenter ContentSource="Header"/>
                            </TextBlock>
                            <ContentPresenter Grid.Row="1" Margin="{TemplateBinding Padding}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
Related Topic