views:

166

answers:

3

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)

+1  A: 

try this:

<DockPanel.Resources>
    <Style TargetType="{x:Type GroupBox}" >
        <Setter Property="Foreground" Value="Red" />
    </Style>
</DockPanel.Resources>

You don't need a templet for this. But if you demand on using a Templete, you probably have to set the Groupbox.HeaderTemplet not the GroupBox.Templet.

Edit:

This is what i got so far, but i keep getting an XamlPraseException.

<Style TargetType="{x:Type GroupBox}" >
    <Setter Property="HeaderTemplate">
        <Setter.Value>
            <ControlTemplate>
                <StackPanel>
                    <StackPanel.Resources>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Foreground" Value="Red"/>
                        </Style>
                    </StackPanel.Resources>
                    <TextBlock Text="{TemplateBinding GroupBox.Header}" />
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Marcel Benthin
That almost works, but it turns into red every TextBlock inside the GroupBox. Besides that, in the code shown in the question I'm simplifying my scenario a bit... actually I'd like to set the "Style" property of the TextBlock conforming the header of the GroupBox.
gschuager
Thanks for the pointer to GroupBox.HeaderTemplate
gschuager
Sry, i couldn't help you, but i'm getting an odd error on my Solution(which is similar to Yacoders answer(except it isn't compiling), but i guess i can stop working on that. One solution should be enough).
Marcel Benthin
I use a tool named Kaxaml to debug such XAML snippets. It's small and handy to play with some style/templates issues...
Yacoder
The problem is that the HeaderTemplate doesn't accept an ControlTemplate, but if i change to DataTemplate the Templeatebinding don't work. ... I don't care ... I don't need that code working ;)
Marcel Benthin
+1  A: 

It seems that the ContentPresenter doesn't use TextBlock to show the string you provide as header or explicitly sets its style, so the style you defined cannot be applied.

If you are certain that you will only use text as group box header, you can remove the ContentPresenter and use a TextBlock on your own.

  <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>
                <TextBlock Text="{TemplateBinding Header}"></TextBlock>
              </Border>
              <ContentPresenter />
            </DockPanel>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </DockPanel.Resources>

  <GroupBox VerticalAlignment="Top"
      Header="GroupBox header"
      DockPanel.Dock="Top"/>
  </DockPanel>
Yacoder
What is that "<ContentPresenter />" for?
Marcel Benthin
Oh, this question deserves a large separate answer :) But, basically a ContentPresenter will allow you to have almost any type of content in the header: text, images, graphical shapes, etc. While this current solution will only handle a text header.http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a2988ae8-e7b8-4a62-a34f-b851aaf13886#contentpresenter
Yacoder
I know what the ContentPreseneter is for, but i don't see the need for that in your Answer.
Marcel Benthin
I see, you mean the default content presenter. You still need it if you want to show the group box content.
Yacoder
i see ... thx ... I should have seen this by my own.
Marcel Benthin
A: 

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.

gschuager