TextBlock 从动态资源中换行

发布于 2024-12-15 17:32:16 字数 386 浏览 2 评论 0原文

有一些资源标记(
 类似于 \r\n)

    <Application.Resources>
      <system:String x:Key="key1">Line1&#x0d;&#x0a;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北音执念 2024-12-22 17:32:19

TextBlock 在使用其 Text 属性时忽略空格。添加换行符的唯一方法是使用 Inlines 属性。虽然这是一个无法直接设置的只读属性,但它也是 TextBlock 的内容属性,因此可以如下设置:

<TextBlock>
    <StaticResource ResourceKey="key1" />
</TextBlock>

您将无法使用 DynamicResource 不过,因为 Inlines 不是依赖属性。

另外,要在 XML 中保留空格,您需要将 xml:space="preserve" 添加到字符串中(xml 是预定义的命名空间,无需声明它):

<system:String xml:space="preserve" x:Key="key1">Line1
Line2</system:String>

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 the TextBlock, and thus can be set like so:

<TextBlock>
    <StaticResource ResourceKey="key1" />
</TextBlock>

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):

<system:String xml:space="preserve" x:Key="key1">Line1
Line2</system:String>
恋竹姑娘 2024-12-22 17:32:16

只需像这样在字符串资源中设置 xml:space="preserve" ,它就会按预期工作 -

<system:String xml:space="preserve" x:Key="key1">Line1
Line2</system:String>

Simply set the xml:space="preserve" in your string resource like this and it will work as expected -

<system:String xml:space="preserve" x:Key="key1">Line1
Line2</system:String>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文