WPF 4 ContentPresenter TextWrapping 样式不适用于隐式生成的 TextBlock
如果我将一段文本分配给 ContentPresenter
的 Content
属性,则 ContentPresenter
会生成一个 TextBlock
控件> 在渲染时包含该文本。
如果我创建适用于 TextBlock
属性的样式并将其分配给该 ContentPresenter
,则该样式似乎不适用于隐式生成的 TextBlock
。
<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
(例如,将样式声明为 TargetType="TextBlock"< /code> 没有
Key
)?
If I assign a piece of text to the Content
property of a ContentPresenter
, a TextBlock
control is generated by the ContentPresenter
at render time to contain that text.
If I create a style that applies toTextBlock
properties and assign it to that ContentPresenter
, the does not appear to apply to the implicitly generated 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}"/>
Is there a way to apply this style successfully to the autogenerated TextBlock
s short of applying it to all TextBlock
s (e.g. declaring style as TargetType="TextBlock"
with no Key
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以这样做......
然后在您定义
ContentPresenter
的位置...TargetType
已设置,因为您知道ContentPresenter
并不总是在其中保存TextBlock
。You can do this...
...then where you define your
ContentPresenter
...The
TargetType
is set since as you know theContentPresenter
will not always hold aTextBlock
in it.如果您没有在其他地方使用该样式,则可以将其直接应用到内容演示器:
If you are not using the style elsewhere, you could apply it directly to the content presenter: