文本块,文本属性中的换行符

有没有办法让 \nTextBlock中断行?

<TextBlock Text="line1\nLine2" />

或者在 Text属性内部有更好的方法来强制中间的换行吗?

<LineBreak />

这对我来说不起作用,它需要是 Text属性的值,因为文本字符串是从外部来源设置的。

我对 LineBreak很熟悉,但这不是我想要的答案。

104298 次浏览

Try this:

<TextBlock>
line1
<LineBreak />
line2
</TextBlock>

How about breaking the line into two tags?

<StackPanel>
<TextBlock Text="Line1" />
<TextBlock Text="Line2" />
</StackPanel>

I know this is ressurecting an old question, but I had the same problem. The solution for me was to use HTML encoded line feeds (&amp;#10;).

Line1&amp;#10;Line2

Looks like

Line1
Line2

For more of the HTML encoded characters check out w3schools

I was having a similar problem and wanted to bind a String of xaml markup to a TextBlock. Essentialy storing the declarative markup inside a TextBlock in a string for later use.

This is how I did: I subclassed the TextBlock to make the InlineCollection bindable and wrote a Converter between the string and an InlineCollection(or actually a generic list of Inlines.)

Correct way to use it may be the following :

<TextBlock>
<Span>text1</Span>
<LineBreak/>
<Span>text2</Span>
</TextBlock>
  <HyperlinkButton
Content="Apply and restart this pplication!&#10;&#13;Note that modifying these settings requires the application to be restarted."   />

CRLF simple way = !&#10;&#13;

!&#10;&#13; - Work on all wpf, xaml, silverlight controls like TextBlock, HyperlinkText and more

just use the AccessText control. you can use it like a label and you have the property TextWrapping="WrapWithOverflow"

Mine is like that and it's working fine. Also, you don't have any problems on changing the text dinamically.

The easiest way is

<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>

So you just write XAML code, and the <LineBreak /> has exactly the same meaning the
in HTML or the "\n" in C#.

If you are binding TextBlock's Text, none of the other answers work. Simply add '\n' to the binding text to where you want to break.

I'm late to the party but .. this is more or less how I did it ,(mind my ItemSources are plain strings, not formatted , and I didn't need to 'convertBack' anything)

public class SpaceToLineBreakConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return (!String.IsNullOrEmpty(value as string))
? new Regex(@"\s").Replace(value as string, "\n")
: value;
}


public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

<LineBreak/> will not work if it is inside a collection such as Grid or StackPanel. In such cases the following would work as shown:

LineBreak inside a collection

This also works fine:

<TextBlock>
<Run Text="My nice text"/>
<LineBreak/>
<LineBreak/>
<Run Text="After some linebreaks, I'm back!"/>
</TextBlock>

this &amp;#10; did not work for me, when I used binding. But this works:

$"first line {Environment.NewLine} second line"

The Best way that worked for me for multiple lines in the same Textblock is:

<TextBlock>
text1
<LineBreak/>
text2
</TextBlock>

Make sure to not use TextWrapping="Wrap". Use TextWrapping="NoWrap" or use nothing.

This also works fine.

Using this method we can modify the text properties in each line as we required.

<TextBlock>
<StackPanel>
<TextBlock FontSize="12" FontWeight="Bold" >My Text One</TextBlock>
<TextBlock FontFamily="Times New Roman" FontStyle="Italic">
- My Text Two
</TextBlock>
</StackPanel>
</TextBlock>