3

aが宣言されている場合、 aControlTemplateを黄色に設定する必要があります。GroupBoxTextBlockHeaderBackground

問題は、 forでTextBlocks のスタイルを定義しても、WPF によって自動生成される s以外には適用されないことです。ContentPresenterHeaderTextBlock

コードは次のとおりです。

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

試してみるとわかるように、2 番目のTextBlock名前付きの背景は黄色ではありません。これは、のリソースで定義されたスタイルが適用されていないことを意味します。notStyledTextBlockGroupBoxContentPresenterControlTemplate

驚いたことに、最初のヘッダー テキストのコンテナーとして WPF によって自動生成されたものGroupBoxは、背景が黄色になっています。

私のスタイルが に適用されるようにするにはどうすればよいnotStyledTextBlock TextBlockですか?

4

1 に答える 1

2

GroupBoxes と ContentPresenter にも問題がありました。質問を投稿しましたが、回答が得られなかったので、少し調べてみました。おそらく同じ問題です(追加情報:実際の問題コードは投稿していませんが、再現に使用できる単純な例を投稿しています)。

于 2010-09-09T12:31:30.067 に答える