TextBlock 从动态资源中换行
有一些资源标记(
 类似于 \r\n)
<Application.Resources>
<system:String x:Key="key1">Line1
Line2</system:String>
</Application.Resources>
和主窗口:
<Grid>
<TextBlock Text="{DynamicResource key1}"/>
<Grid>
但结果只有一行:“Line1 Line2”。怎么了?
There is some markup for resources ( is a analog of \r\n)
<Application.Resources>
<system:String x:Key="key1">Line1
Line2</system:String>
</Application.Resources>
and for main window:
<Grid>
<TextBlock Text="{DynamicResource key1}"/>
<Grid>
But the result is only one line: "Line1 Line2". What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TextBlock
在使用其 Text 属性时忽略空格。添加换行符的唯一方法是使用 Inlines 属性。虽然这是一个无法直接设置的只读属性,但它也是TextBlock
的内容属性,因此可以如下设置:您将无法使用
DynamicResource
不过,因为 Inlines 不是依赖属性。另外,要在 XML 中保留空格,您需要将
xml:space="preserve"
添加到字符串中(xml
是预定义的命名空间,无需声明它):TextBlock
ignores whitespace when using its Text property. The only way to add line breaks is to use the Inlines property. While this is a read-only property that cannot be set directly, it is also the content property of theTextBlock
, and thus can be set like so:You will not be able to use
DynamicResource
though, since Inlines is not a dependency property.Also, for whitespace to be preserved in XML, you will need to add
xml:space="preserve"
to your string (xml
is a predefined namespace, no need to declare it):只需像这样在字符串资源中设置
xml:space="preserve"
,它就会按预期工作 -Simply set the
xml:space="preserve"
in your string resource like this and it will work as expected -