How to change default properties for TextBlock for ContentPresenter in a template

Based on the following code:

<GroupBox>
  <GroupBox.Template>
    <ControlTemplate TargetType="{x:Type GroupBox}">
      <ContentPresenter TextElement.FontSize="28" />
    </ControlTemplate>
  </GroupBox.Template>
  <TextBlock>Test</TextBlock>
</GroupBox>

I expected "Test" to display with FontSize = 28. But instead, it uses the default size.

If I remove the TextBlock as follows:

<GroupBox>
  <GroupBox.Template>
    <ControlTemplate TargetType="{x:Type GroupBox}">
      <ContentPresenter TextElement.FontSize="28" />
    </ControlTemplate>
  </GroupBox.Template>
  Test
</GroupBox>

Now the text is displayed with 28 as FontSize.

Should I not inherit the value of a property when I use TextBlock?

This other question How do I change FontFamily to ContentPresenter? doesn't help as it only works for default content.

This question also: How to change FontFamily to ContentPresenter? .

Both work if you use the default content handler, but fail to create the text block manually.

: , ContentControl:

<StackPanel>
   <StackPanel.Resources>
      <ControlTemplate x:Key="UsingBorderTemplate" TargetType="{x:Type ContentControl}">
         <Border BorderBrush="Red" BorderThickness="1" TextElement.FontFamily="Courier New" Margin="5">
            <ContentPresenter/>
         </Border>
      </ControlTemplate>
      <ControlTemplate x:Key="MyTemplate" TargetType="{x:Type ContentControl}">
         <ContentPresenter TextElement.FontFamily="Courier New" Margin="5" />
      </ControlTemplate>
   </StackPanel.Resources>
   <ContentControl Template="{StaticResource MyTemplate}">
        I'm courier new!
   </ContentControl>
   <ContentControl Template="{StaticResource MyTemplate}">
       <TextBlock>I'm default!</TextBlock>
   </ContentControl>
</StackPanel>

"MyTemplate" "UseBorderTemplate" .

+2
2

ContentPresenter. , , . , .

, , .

+2

, , , - GroupBox.Header, TextBox , .

, :

<GroupBox.Header>Test</GroupBox.Header> 

,
Berryl

0

All Articles