WPF templating/styling issue

stylingtemplatingwpf

Given this piece of XAML

<DockPanel>
  <DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type GroupBox}">
            <DockPanel>
              <Border DockPanel.Dock="Top">
                <Border.Resources>
                  <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Foreground"
                        Value="Red" />
                  </Style>
                </Border.Resources>
                <ContentPresenter ContentSource="Header" />
              </Border>
              <ContentPresenter />
            </DockPanel>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DockPanel.Resources>

  <GroupBox VerticalAlignment="Top"
      Header="GroupBox header"
      DockPanel.Dock="Top">

    ...
    ...

I would like to know why the group box header is not displayed in red letters.

I've already tried styling the Label type with no success either.

(sorry about the overly generic post title… I wasn't able to think of something more meaninful)

Best Answer

This code solved the problem:

<DockPanel>
  <DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}">
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <DataTemplate.Resources>
              <Style TargetType="Label">
                <Style.Setters>
                  <Setter Property="Foreground" Value="Red" />
                </Style.Setters>
              </Style>
            </DataTemplate.Resources>
            <Label Content="{Binding}" />
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DockPanel.Resources>

  <GroupBox VerticalAlignment="Top" Header="GroupBox header" DockPanel.Dock="Top">
  ...
  ...

However, I still don't know why the proposed code didn't worked.

Related Topic