2010-09-09 78 views
3

我想爲GroupBox製作ControlTemplate,這樣如果在Header中聲明瞭TextBlock,則應將其Background設置爲黃色。WPF GroupBox ControlTemplate:如何僅將樣式應用於標題中的元素?

的問題是,雖然我在ContentPresenterHeader定義TextBlock秒的風格,它不是應用除了那些TextBlock S的由WPF自動生成。

下面是代碼:

<Window 
    x:Class="TestHeaderTemplate.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" 
    SizeToContent="WidthAndHeight"> 
    <Window.Resources>  
    <Style 
     TargetType="{x:Type GroupBox}"> 
     <Setter 
     Property="Template"> 
     <Setter.Value> 
      <ControlTemplate 
      TargetType="{x:Type GroupBox}">    
      <Border 
       Margin="{TemplateBinding Margin}" 
       BorderBrush="Black" 
       BorderThickness="1"> 
       <StackPanel> 
       <Border 
        Margin="0,0,0,5" 
        BorderThickness="5" 
        BorderBrush="LightBlue" 
        > 
        <ContentPresenter 
        ContentSource="Header"> 
        <ContentPresenter.Resources> 
         <Style 
         TargetType="{x:Type TextBlock}"> 
         <Setter 
          Property="Background" 
          Value="Yellow" /> 
         </Style> 
        </ContentPresenter.Resources> 
        </ContentPresenter> 
       </Border> 
       <ContentPresenter 
        ContentSource="Content" /> 
       </StackPanel> 
      </Border> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </Window.Resources> 
    <StackPanel> 
    <TextBox 
     Text="All TextBoxes in a GroupBox's Header should be yellow, whether declared or autogenerated." /> 
    <GroupBox 
     x:Name="firstGroupBox" 
     Margin="5" 
     Header="I am a TextBlock autogenerated by WPF. Since I'm in the Header, I should be yellow."> 
     <TextBlock 
      Text="I'm a TextBlock declared in the content of the GroupBox. I should NOT be yellow." />  
    </GroupBox> 
    <GroupBox 
     x:Name="secondGroupbox" 
     Margin="5" 
     > 
     <HeaderedContentControl.Header>  
      <TextBlock 
      x:Name="notStyledTextBlock" 
      Text="I'm a TextBlock declared in the header. I should be yellow since I'm in the header." 
      VerticalAlignment="Center" />   
     </HeaderedContentControl.Header> 
     <TextBlock 
     Text="I'm declared in the content so I should not be yellow." /> 
    </GroupBox> 
    </StackPanel> 
</Window> 

正如你可以看到,如果你試試,TextBlock命名notStyledTextBlock第二GroupBox的背景不是黃色,這意味着在ContentPresenter的資源定義的樣式在ControlTemplate不適用。

令人驚訝的是,WPF自動生成的作爲第一個GroupBox的標題文本的容器的背景爲黃色。

我該怎麼做才能讓我的風格適用於notStyledTextBlockTextBlock

+0

將樣式中的邊框背景設置爲黃色將在您的示例中修復它,但也會爲標頭中使用的非文本塊添加黃色背景。我贊成HCL的回答,因爲我相信他給出的解釋,這是設計。 – 2010-09-09 13:24:11

回答

2

我也有GroupBoxes和ContentPresenter的問題。我發佈了一個問題,因爲沒有給出答案,我調查了一下自己。看看this的答案,也許它是相同的問題(附加信息:我沒有發佈我真正的問題代碼,但可以用來複制的簡化示例)。

+0

你是對的,它有相同的問題:特別是這個部分:「如果ContentControl的內容已經是WPF元素,它在ContenPresenter中使用之前創建」< - 這可能是爲什麼樣式不適用到第二個TextBlock。然而,我看不到一種方法來告訴WPF,我希望某種樣式僅適用於標題。 ControlTemplate.Resources不起作用,因爲該樣式將應用於所有TextBlocks(包含在標題和內容中)。我希望有一個ContentPresenter.ContentStyle屬性... – Pequatre 2010-09-09 12:38:17

+0

我已經標記了你的答案被接受,因爲我還沒有能夠在這個問題上取得任何進展。也感謝WallStreet Programmer的支持。 – Pequatre 2010-09-14 11:36:25

相關問題