views:

717

answers:

1

I defined a DataTemplate for the header of my GroupBoxes:

   <Style x:Key="GroupBoxHeaderStyle" TargetType="{x:Type GroupBox}">
        <Setter Property="Foreground" Value="White"></Setter>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate>                       
                    <TextBlock Text="{Binding}" Foreground="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Foreground}"
                               FontSize="18" FontFamily="Global User Interface"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

However the binding done for the foreground doesn't seem to work. The headers of my groupBoxes are always black. What am I doing wrong?

This is how I'm defining the GroupBox:

<GroupBox Header="Views" Margin="1" Foreground="White"
          Style="{StaticResource GroupBoxHeaderStyle}">
          ...
+3  A: 
...
<TextBlock Text="{Binding}" Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Foreground}"
           FontSize="18" FontFamily="Global User Interface"/>
...
Thomas Levesque
You beat me to it by a hair! Anyway, to expand on this, the TemplatedParent binding that was being used isn't working because the TemplatedParent is the Header, not the GroupBox. Find Ancestor is the correct way to retrieve the GroupBox's properties.
rmoore
Actually TemplatedParent is for use in ControlTemplates, I don't think it works in a DataTemplate.
Thomas Levesque
It will work for any thing deriving from FrameworkTemplate, but I agree that it rarely makes sense to use it outside of of ControlTemplates, same with TemplateBindings. You can test it out by changing the Text in the TextBlock to Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
rmoore