开发者

How to change TextBlock default properties for a 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 was expecting "Test" to be displayed with FontSize=28. But it uses the default size instead.

If I remove the TextBlock like this :

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

The text is now the displayed with 28 as FontSize.

Shouldn't the property value be inherited when I use a TextBlock ?

This other question How do I Change the FontFamily on a ContentPresenter? doesn't help, as it works only for default content too.

This question also : How do I Change the FontFamily on a ContentPresenter?.

Both works whe you use the default content handler, but fails when you manually create a textblock.

Edit: As demonstrated in this other question, I've tried by simply using a ContentControl :

<StackPanel>
   <StackPanel.Resources>
      <ControlTemplate x:Key="UsingBorderTemplate" TargetType="{x:Type ContentControl}">
         <Border BorderBrush="Red" BorderThickness="1" TextElement.FontFamily="C开发者_开发百科ourier 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>

You can change the template from "MyTemplate" to "UsingBorderTemplate" with the same result.


I had an odd problem with ContentPresenter. I remember that I have analyzed the source of the problem and have found out that it was by design - Probably you have here the same issue.

Look at this post, maybe it helps you.


I think the text that the content presenter is presenting is the GroupBox.Header, and you may just be tacking another TextBox in there that isn't part of the Group Box.

In your first code block, add the line below and see if that works:

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

HTH,
Berryl

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜