gpt4 book ai didi

WPF 4 ContentPresenter TextWrapping 样式不适用于隐式生成的 TextBlock

转载 作者:行者123 更新时间:2023-12-04 02:32:22 27 4
gpt4 key购买 nike

如果我将一段文本分配给 Content ContentPresenter 的属性, TextBlock控制由 ContentPresenter 生成在渲染时包含该文本。

如果我创建一个适用于 TextBlock 的样式属性并将其分配给该 ContentPresenter ,似乎不适用于隐式生成的 TextBlock s。

<Style x:Key="SampleStyle">
<Setter Property="TextBlock.TextWrapping" Value="Wrap"/>
</Style>

<ContentPresenter Content="This is a Test piece of text." Style="{StaticResource SampleStyle}"/>

有没有办法将此样式成功应用于自动生成的 TextBlock缺少将其应用于所有 TextBlock s(例如,将样式声明为 TargetType="TextBlock" 而没有 Key )?

最佳答案

你可以这样做...

<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type TextBlock}" x:Key="WrappingStyle">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</ResourceDictionary>
</Window.Resources>

...然后你在哪里定义你的 ContentPresenter ...
<ContentPresenter Content="This text is going to wrap...">
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource WrappingStyle}"/>
</ContentPresenter.Resources>
</ContentPresenter>
TargetType正如您所知,已设置 ContentPresenter不会总是持有 TextBlock在里面。

关于WPF 4 ContentPresenter TextWrapping 样式不适用于隐式生成的 TextBlock,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3970285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com