WPF标签中的换行符?

WPF标签中的换行符?,wpf,label,newline,Wpf,Label,Newline,如何在WPF中的标签文本中添加换行符,如下所示 <Label>Lorem ipsum</Label> Lorem 乱数假文 Loremipsum 您需要使用TextBlock,因为TextBlock作为子对象接受内联对象的集合。因此,您为TextBlock元素提供了三个内联项:runtext=“Lorem”、LineBreak和runtext=“ipsum” 您不能执行以下操作: <Label>Lorem<LineBreak/>ipsum

如何在WPF中的标签文本中添加换行符,如下所示

<Label>Lorem 
  ipsum</Label>
Lorem
乱数假文
Loremipsum
您需要使用TextBlock,因为TextBlock作为子对象接受内联对象的集合。因此,您为TextBlock元素提供了三个内联项:runtext=“Lorem”、LineBreak和runtext=“ipsum”

您不能执行以下操作:

<Label>Lorem<LineBreak/>ipsum</Label>`
Loremipsum`
因为标签接受一个内容子元素


另外,不确定您的用例是什么,但请注意,我在Label元素中放置了一个TextBlock。它是重复的吗?不完全是,取决于你的需要。这是一篇关于这两个元素之间差异的好文章:

在WPF中,您可以使用值
“
;”
“
;”

例如:

<Label Content="Lorem&#10;ipsum" />

(“10”是换行符的ASCII码)


(“A”是十六进制换行符的ASCII数字)


在ViewModel或Model中执行此操作时,我发现使用Environment.NewLine具有最一致的结果,包括本地化。它也应该直接在视图中工作,但我还没有对此进行测试

例如:

在视图中

<Label Content="{Binding SomeStringObject.ParameterName}" />

有关如何向控件(例如按钮)添加具有多行的工具提示的示例。工具提示的宽度有限,因此如果句子太宽,它将自动换行

<!-- Button would need some properties to make it clickable.-->
<Button>
   <Button.ToolTip>
      <TextBlock Text="Line 1&#x0a;Line 2" MaxWidth="300" TextWrapping="Wrap"/>
    </Button.ToolTip>
</Button>

在VS2019+.NET 4.6.1+WPF上测试。

文本内容
<Label xml:space="preserve">text content
another line</Label>
另一行

似乎也起作用了

当我尝试上面的答案时,我的内容中间有一个标签,我得到了一个错误。相反,我使用了:
First Second
。在这种情况下,使用
Lorem
;Ipsum
同意这应该是公认的答案。它更优雅,使用更简单。
SomeStringObject.ParameterName = "First line" + Environment.NewLine + "Second line";
<!-- Button would need some properties to make it clickable.-->
<Button>
   <Button.ToolTip>
      <TextBlock Text="Line 1&#x0a;Line 2" MaxWidth="300" TextWrapping="Wrap"/>
    </Button.ToolTip>
</Button>
<Label xml:space="preserve">text content
another line</Label>