ControlTemplate 中的 ContentPresenter 无法更改附加的依赖属性
为什么下面的简化代码没有将 TextBlock 的 font-size 设置为 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 会以我想要的大小显示文本。编译或执行应用程序后,文本块的大小始终重置为其默认大小。
我还测试了具有样式和嵌入资源的各种版本,但最终总是处于无法从包含 ContentPresenter 的 ControlTemplate 中设置继承附加 dp 的情况。这是设计使然吗?
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>
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了此行为的原因 - 这是设计使然:
如果 ContentControl 的内容已经是 WPF 元素,则它是在 ContenPresenter 中使用它之前创建的。元素的逻辑父级是,因此ContentControl。我可以通过将 ContentControl 标记更改为以下内容来检查这一点:
在此示例中,文本大小为所需的 50。我也可以用 Visual Studio 的 wpf-visualizer 来证明这个论点。父级是ContentControl,通过dp继承,FontSize是从父级(ContentControl)获取的,并且文本以50的大小显示!
如果 ContentControl 仅包含文本作为内容,则可以观察到另一种行为:
在这种情况下,TextBox 是通过 ContentPresenter 创建的,因为文本不能进入视觉树。文本框没有父级,但 TemplateParent 属性导致 ContentPresenter 作为 TextBoxes 父级,并且 DP 系统通过从 ContentPresenter 继承的附加依赖属性继承获取 FontSize 值。这就是为什么在这种情况下字体大小更改为 50。
不同的情况如下所述 这里。
我不明白的是,为什么VS2010在编译前显示FontSize 50。
I found the reason of this behavior - it’s by design:
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:
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!
Another behavior can be observed if the ContentControl contains only text as content:
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.
The different scenarios are described here.
What I don’t understand is, why VS2010 shows the FontSize 50 before compiling.
怎么样:
How about :
这很有趣,因为我已经得到了这样的东西来工作。有区别吗?
在xaml页面中我有:
也许我们必须使用数据模板才能获得所需的效果?
That's interesting because I have gotten something Like this to work. Is there a difference?
In xaml page I have:
Perhaps we HAVE to use data templates to get the desired effect?