2009-06-08 58 views
1

我定義了我的GroupBoxes的頭一個DataTemplate:問題結合分組框中的前景屬性HeaderTemplate中

<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 Header="Views" Margin="1" Foreground="White" 
      Style="{StaticResource GroupBoxHeaderStyle}"> 
      ... 

回答

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

您的頭髮打我吧!無論如何,爲了擴展此,正在使用的TemplatedParent綁定不起作用,因爲TemplatedParent是標題,而不是GroupBox。 Find Ancestor是檢索GroupBox屬性的正確方法。 – rmoore 2009-06-08 23:49:37