99
votes

Is there a way to have \n make a line break in a TextBlock?

<TextBlock Text="line1\nLine2" />

Or is there a better way to force a middle line break, inside the Text attribute?

<LineBreak />

This doesn't work for me, it needs to be the value of the Text attribute, because the text string is being set from an outside source.

I'm familiar with LineBreak but it's not the answer I'm looking for.

15
in xaml you have to use control characters <TextBlock Name="txtBlock" Text="Line1&#10;Line2" />, if you want to use the \n character it only works from code behind txtBlock.Text = "line1\nline2";JJ_Coder4Hire
&#10; works fine in xaml. Thank you JJ_Coder4Hire !Kay Lee

15 Answers

119
votes

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

128
votes

Try this:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>
10
votes

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#.

5
votes

How about breaking the line into two tags?

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

<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

5
votes

Correct way to use it may be the following :

<TextBlock>  
    <Span>text1</Span>  
    <LineBreak/>  
    <Span>text2</Span>  
</TextBlock>
3
votes
  <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

3
votes

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.

3
votes

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.

2
votes

This also works fine:

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

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

eg.

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

1
votes

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();
    }
}
1
votes

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

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

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