绑定GroupBox HeaderTemplate的Foreground属性时出现问题

时间:2009-06-08 23:34:48

标签: c# wpf datatemplate groupbox

我为GroupBoxes的标题定义了一个DataTemplate:

   <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>

然而,为前景进行的绑定似乎不起作用。我的groupBoxes的标题总是黑色的。我究竟做错了什么?

这就是我定义GroupBox的方式:

<GroupBox Header="Views" Margin="1" Foreground="White"
          Style="{StaticResource GroupBoxHeaderStyle}">
          ...

1 个答案:

答案 0 :(得分:3)

...
<TextBlock Text="{Binding}" Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}, Path=Foreground}"
           FontSize="18" FontFamily="Global User Interface"/>
...