如何更改模板中 ContentPresenter 的 TextBlock 默认属性 [英] How to change TextBlock default properties for a ContentPresenter in a template
问题描述
基于以下代码:
<GroupBox>
<GroupBox.Template>
<ControlTemplate TargetType="{x:Type GroupBox}">
<ContentPresenter TextElement.FontSize="28" />
</ControlTemplate>
</GroupBox.Template>
<TextBlock>Test</TextBlock>
</GroupBox>
我期望以 FontSize=28 显示Test".但它使用默认大小.
I was expecting "Test" to be displayed with FontSize=28. But it uses the default size instead.
如果我像这样删除 TextBlock :
If I remove the TextBlock like this :
<GroupBox>
<GroupBox.Template>
<ControlTemplate TargetType="{x:Type GroupBox}">
<ContentPresenter TextElement.FontSize="28" />
</ControlTemplate>
</GroupBox.Template>
Test
</GroupBox>
文本现在显示为 28 作为 FontSize.
The text is now the displayed with 28 as FontSize.
当我使用 TextBlock 时,属性值不应该被继承吗?
Shouldn't the property value be inherited when I use a TextBlock ?
另一个问题 我如何更改 FontFamilyContentPresenter? 没有帮助,因为它也只适用于默认内容.
This other question How do I Change the FontFamily on a ContentPresenter? doesn't help, as it works only for default content too.
这个问题还有:如何更改 FontFamily内容展示者?.
当您使用默认内容处理程序时两者都有效,但在您手动创建文本块时失败.
Both works whe you use the default content handler, but fails when you manually create a textblock.
如另一个问题所示,我已经尝试过简单地使用 ContentControl :
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="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"更改为UsingBorderTemplate",结果相同.
You can change the template from "MyTemplate" to "UsingBorderTemplate" with the same result.
推荐答案
我在使用 ContentPresenter 时遇到了一个奇怪的问题.我记得我已经分析了问题的根源并发现它是设计使然 - 可能您在这里遇到了同样的问题.
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.
这篇关于如何更改模板中 ContentPresenter 的 TextBlock 默认属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!