108

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.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
ScottCate
  • 2,365
  • 7
  • 21
  • 35

16 Answers16

135

Try this:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
123

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

croxy
  • 4,082
  • 9
  • 28
  • 46
Noaki
  • 1,673
  • 2
  • 14
  • 16
11

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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stephane Halimi
  • 408
  • 3
  • 5
8

<LineBreak/>

http://www.longhorncorner.com/UploadFile/mahesh/XamlLineBreak06092005152257PM/XamlLineBreak.aspx

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • 3
    Scott I don't understand your comment. – jcollum May 11 '09 at 15:26
  • 1
    The problem is that doesn't work on Windows XP. It may also have something to do with the .NET version installed. There are no exceptions and no errors other than the visual elements don't display correctly. – Charles Dec 15 '09 at 21:29
  • @Charles If the problem was the .NET version why didn't you just install an update? I don't care about old libraries if you can update them without any trouble. – The incredible Jan Aug 12 '22 at 07:44
7

<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

user2063329
  • 443
  • 2
  • 5
  • 15
  • I tried this as the content for a RadioButton. Everything seemed to work fine until I ran the program on Windows 7. On windows 7 the button was centered vertically between the two lines. Windows 10 aligns the button top the top by default. While Windows 10 would honor the VerticalContentAlignment property, Windows 7 ignores it. I had to use the LineBreak solution. – mrfelis Feb 14 '18 at 16:35
  • @mrfelis How can a button get between 2 text blocks that are it's content? And why did you even use this solution here? A RadioButton is no collection, is it? Could you please show your code? – The incredible Jan Aug 12 '22 at 07:54
  • @TheincredibleJan It has been 4 years, so no, I don't have the code. It probably looked something like this: `Line 1Line 2Line 3`. I also don't have a Windows 7 machine to test with either, but I would presume that on Windows 7, the button always aligned with 'Line 2' regardless of any alignment settings – mrfelis Aug 30 '22 at 12:54
7

How about breaking the line into two tags?

<StackPanel>
    <TextBlock Text="Line1" />
    <TextBlock Text="Line2" />
</StackPanel>
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
6

Correct way to use it may be the following :

<TextBlock>  
    <Span>text1</Span>  
    <LineBreak/>  
    <Span>text2</Span>  
</TextBlock>
radu florescu
  • 4,315
  • 10
  • 60
  • 92
  • Aside from the fact that that doesn't answer the question (outside source) - what's "more correct" with Span than without? – The incredible Jan Aug 12 '22 at 08:07
  • @TheincredibleJan You are writing a comment that is not related to the content. And you might overlook that was a solution for 2017 not 2022. – radu florescu Mar 04 '23 at 18:32
6

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.

5
  <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

user1551704
  • 94
  • 1
  • 3
5

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.

newman
  • 6,841
  • 21
  • 79
  • 126
4

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

$"first line {Environment.NewLine} second line"
Welcor
  • 2,431
  • 21
  • 32
3

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();
    }
}
Dan
  • 2,818
  • 23
  • 21
3

This also works fine:

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

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.

MelloG
  • 1,044
  • 1
  • 11
  • 11
2

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

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124
2

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>
Toni
  • 1,555
  • 4
  • 15
  • 23