ControlTemplate中的ContentPresenter无法更改附加的依赖项属性 [英] ContentPresenter within ControlTemplate cannot change attached dependency property
问题描述
为什么下面的简化代码没有将TextBlock的字体大小设置为50?
Why does the following simplified code not sets the font-size of the TextBlock to 50?
<Window.Resources>
<ControlTemplate TargetType="ContentControl" x:Key="Test">
<ContentPresenter TextBlock.FontSize="50" />
</ControlTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource Test}">
<TextBlock>Test should be rendered big</TextBlock>
</ContentControl>
</Grid>
如果更改FontSize属性的值,Visual Studio将以所需大小显示文本.编译或执行应用后,文本块的大小始终会重置为其默认大小.
If I change the value of the FontSize property, visual studio shows me the text in the size I want. After compiling or executing the app, the size of the textblock is always reset to its default size.
我还测试了具有样式和嵌入式资源的各种版本,但是我总是遇到无法从包含ContentPresenter的ControlTemplate中设置继承附加dp的情况.这是设计使然吗?
I have also tested various versions with styles and embedded resources but I end always in the situation that I cannot set inheriting attached dp's from within a ControlTemplate that contains a ContentPresenter. Is this by design?
推荐答案
我发现了这种现象的原因-这是设计使然:
I found the reason of this behavior - it’s by design:
如果ContentControl的内容已经是WPF元素,则在创建它之前先在ContenPresenter中使用它.元素的逻辑父级因此是 ContentControl .我可以通过将ContentControl-markup更改为以下内容来进行检查:
If the Content of ContentControl is already a WPF-Element, it is created before using it in the ContenPresenter. The logical parent of the element is therefore ContentControl. I can check this through changing the ContentControl-markup to the following:
<ContentControl Template="{StaticResource Test}" TextBlock.FontSize="50">
<TextBlock>
This text now is shown with a size of 50
</TextBlock>
</ContentControl>
在此示例中,文本大小为50.我也可以使用Visual Studio的wpf-visualizer证明这一论点.父级是ContentControl,通过dp继承,FontSize取自父级(ContentControl),并且显示的文字大小为50!
In this example the text size is 50 as desired. I can prove this argumentation also with wpf-visualizer of visual studio. The parent is ContentControl and through dp-inheritance, the FontSize is taken from the parent (ContentControl), and the text is shown with a size of 50!
另一种行为:
<Window.Resources>
<ControlTemplate x:Key="Test" TargetType="{x:Type ContentControl}">
<ContentPresenter TextBlock.FontSize="50"/>
</ControlTemplate>
</Window.Resources>
<Grid>
<ContentControl Template="{StaticResource Test}">
This text is shown with a size of 50
</ContentControl>
</Grid>
在这种情况下,文本框是通过ContentPresenter 创建的,因为无法在可视树中输入文本.文本框没有父级,但是TemplateParent属性作为TextBoxes父级导致ContentPresenter,并且DP系统通过从ContentPresenter附加的依赖项属性继承获取FontSize-value.因此,在这种情况下,字体大小更改为50.
In this scenario, the TextBox is created through the ContentPresenter because text cannot be entered in the visual tree. The textbox has no parent but the TemplateParent-property leads to the ContentPresenter as the TextBoxes parent and the DP-system takes the FontSize-value through attached dependency property inheritance from the ContentPresenter. That’s why in this scenario the font size is changed to 50.
我不明白的是,为什么VS2010在编译前会显示FontSize 50.
What I don’t understand is, why VS2010 shows the FontSize 50 before compiling.
这篇关于ControlTemplate中的ContentPresenter无法更改附加的依赖项属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!