312

How can I add a line break to text when it is being set as an attribute i.e.:

<TextBlock Text="Stuff on line1 \n Stuff on line2" />

Breaking it out into the exploded format isn't an option for my particular situation. What I need is some way to emulate the following:

<TextBlock>
  <TextBlock.Text>
    Stuff on line1 <LineBreak/>
    Stuff on line2
  </TextBlock.Text>
<TextBlock/>
0

13 Answers 13

626
<TextBlock Text="Stuff on line1&#x0a;Stuff on line 2"/>

You can use any hexadecimally encoded value to represent a literal. In this case, I used the line feed (char 10). If you want to do "classic" vbCrLf, then you can use &#x0d;&#x0a;

By the way, note the syntax: It's the ampersand, a pound, the letter x, then the hex value of the character you want, and then finally a semi-colon.

ALSO: For completeness, you can bind to a text that already has the line feeds embedded in it like a constant in your code behind, or a variable constructed at runtime.

15
  • 17
    Apparently this does not work within <TextBlock.Text></TextBlock.Text> Tags.
    – tcables
    Dec 22, 2010 at 8:36
  • 59
    As of today, I have 34 upvotes on this answer, and I still had to come back and check because the syntax is so archaic.
    – Bob King
    Mar 7, 2011 at 2:48
  • 4
    Absolutely. I have this bookmarked. I have to come check it all the time.
    – MojoFilter
    Apr 25, 2011 at 18:45
  • 5
    'Archaic' ==> 'Arcane' :-)
    – Will Dean
    Feb 19, 2015 at 14:49
  • 6
    Works even inside a binding parameter when adding a backslash like: ConverterParameter='Test\&#x0a;Text'
    – Sven
    Apr 23, 2015 at 15:28
77

May be you can use the attribute xml:space="preserve" for preserving whitespace in the source XAML

<TextBlock xml:space="preserve">
Stuff on line 1
Stuff on line 2
</TextBlock>
4
  • 11
    You could also remove xml:space="preserve" and add <LineBreak /> between the lines
    – usefulBee
    Sep 17, 2014 at 14:38
  • 5
    It should be noted (a few years later) that xml:space="preserve" will also keep the indent spaces, which are usually present in XAML. Thus an explicit <LineBreak /> as suggested above seems to be a cleaner solution.
    – Zeus
    Aug 5, 2016 at 1:14
  • Thanks! This solution fitted to my XAML-based serialization code very well.
    – Seraphim
    Jan 28, 2017 at 14:11
  • Oh this worked perfect. It saved me so much boiler plate code. thanks
    – iam.Carrot
    Feb 2, 2020 at 21:18
66

When you need to do it in a string (eg: in your resources) you need to use xml:space="preserve" and the ampersand character codes:

<System:String x:Key="TwoLiner" xml:space="preserve">First line&#10;Second line</System:String>

Or literal newlines in the text:

<System:String x:Key="TwoLiner" xml:space="preserve">First line 
Second line</System:String>

Warning: if you write code like the second example, you have inserted either a newline, or a carriage return and newline, depending on the line endings your operating system and/or text editor use. For instance, if you write that and commit it to git from a linux systems, everything may seem fine -- but if someone clones it to Windows, git will convert your line endings to \r\n and depending on what your string is for ... you might break the world.

Just be aware of that when you're preserving whitespace. If you write something like this:

<System:String x:Key="TwoLiner" xml:space="preserve">
First line 
Second line 
</System:String>

You've actually added four line breaks, possibly four carriage-returns, and potentially trailing white space that's invisible...

5
  • 10
    BTW, you can just apply the xml:space="preserve" attribute to the ResourceDictionary too, so that you don't need to apply it to every resource string.
    – ocodo
    Feb 23, 2011 at 0:51
  • 1
    Without the ampersand also works. Something like this: <System:String x:Key="TwoLiner" xml:space="preserve">First line Second line</System:String> (with a line break between First line and Second line)
    – vgaltes
    Jul 7, 2015 at 8:28
  • 1
    I wish I could give you +10 for xml:space="preserve"
    – TimothyP
    Oct 26, 2016 at 9:14
  • 1
    @trooper I just made the edit and it was left up to Jaykul/mod's approval. And Jaykul's further builds really clarified the minor details here (since my edit didn't give the desired output exactly as wanted). Overall I'd say this interaction helps create even better answers rather than posting my own.
    – wooobie
    Mar 28, 2017 at 15:57
  • xml:space="preserve" This helped me. Thank You
    – SkyDancer
    Dec 30, 2021 at 3:13
21

You need just removing <TextBlock.Text> and simply adding your content as following:

    <Grid Margin="20">
        <TextBlock TextWrapping="Wrap" TextAlignment="Justify" FontSize="17">
        <Bold FontFamily="Segoe UI Light" FontSize="70">I.R. Iran</Bold><LineBreak/>
        <Span FontSize="35">I</Span>ran or Persia, officially the <Italic>Islamic Republic of Iran</Italic>, 
        is a country in Western Asia. The country is bordered on the 
        north by Armenia, Azerbaijan and Turkmenistan, with Kazakhstan and Russia 
        to the north across the Caspian Sea.<LineBreak/>
        <Span FontSize="10">For more information about Iran see <Hyperlink NavigateUri="http://en.WikiPedia.org/wiki/Iran">WikiPedia</Hyperlink></Span>
            <LineBreak/>
            <LineBreak/>
            <Span FontSize="12">
                <Span>Is this page helpful?</Span>
                <Button Content="No"/>
                <Button Content="Yes"/>
            </Span>
    </TextBlock>
    </Grid>

enter image description here

1
  • And the question was "How can I add a line break to text when it is being set as an attribute", attribute, not element.
    – mins
    Nov 18, 2019 at 9:22
15

Note that to do this you need to do it in the Text attribute you cannot use the content like

<TextBlock>Stuff on line1&#x0a;Stuff on line 2</TextBlock>
14

Maybe someone prefers

<TextBlock Text="{Binding StringFormat='Stuff on line1{0}Stuff on line2{0}Stuff on line3',
                          Source={x:Static s:Environment.NewLine}}" />

with xmlns:s="clr-namespace:System;assembly=mscorlib".

2
  • Although adding &#x0a; seems to work from XAML, it doesn't from code. This one does.
    – F3RD3F
    Jan 14, 2015 at 9:19
  • I think this is a much better way because different OS expects different characters for the newline. This way, the correct characters are used regardless of what OS the app is running on. Jan 18, 2022 at 12:55
11

For those that have tried every answer to this question and are still scratching their heads as to why none of them work for you, you might have ran into a form of the issue I ran into.

My TextBlock.Text property was inside of a ToolTipService.ToolTip element and it was databound to a property of an object whose data was being pulled from a SQL stored procedure. Now the data from this particular property within the stored procedure was being pulled from a SQL function.

Since nothing had worked for me, I gave up my search and created the converter class below:

public class NewLineConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var s = string.Empty;

        if (value.IsNotNull())
        {
            s = value.ToString();

            if (s.Contains("\\r\\n"))
                s = s.Replace("\\r\\n", Environment.NewLine);

            if (s.Contains("\\n"))
                s = s.Replace("\\n", Environment.NewLine);

            if (s.Contains("&#x0a;&#x0d;"))
                s = s.Replace("&#x0a;&#x0d;", Environment.NewLine);

            if (s.Contains("&#x0a;"))
                s = s.Replace("&#x0a;", Environment.NewLine);

            if (s.Contains("&#x0d;"))
                s = s.Replace("&#x0d;", Environment.NewLine);

            if (s.Contains("&#10;&#13;"))
                s = s.Replace("&#10;&#13;", Environment.NewLine);

            if (s.Contains("&#10;"))
                s = s.Replace("&#10;", Environment.NewLine);

            if (s.Contains("&#13;"))
                s = s.Replace("&#13;", Environment.NewLine);

            if (s.Contains("<br />"))
                s = s.Replace("<br />", Environment.NewLine);

            if (s.Contains("<LineBreak />"))
                s = s.Replace("<LineBreak />", Environment.NewLine);
        }

        return s;
    }

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

I ended up having to use the Enivornment.NewLine method from @dparker's answer. I instructed the converter to look for any possible textual representation of a newline and replace it with Environment.NewLine.

This worked!

However, I was still perplexed as to why none of the other methods worked with databound properties.

I left a comment on @BobKing's accepted answer:

@BobKing - This doesn't seem to work in the ToolTipService.ToolTip when binding to a field that has the line feeds embedded from a SQL sproc.

He replied with:

@CodeMaverick If you're binding to text with the new lines embedded, they should probably be real char 10 values (or 13's) and not the XML sentinels. This is only if you want to write literal new lines in XAML files.

A light bulb went off!

I went into my SQL function, replaced my textual representations of newlines with ...

CHAR( 13 ) + CHAR( 10 )

... removed the converter from my TextBlock.Text binding, and just like that ... it worked!

9

I have found this helpful, but ran into some errors when adding it to a "Content=..." tag in XAML.

I had multiple lines in the content, and later found out that the content kept white spaces even though I didn't specify that. so to get around that and having it "ignore" the whitespace, I implemented such as this.

<ToolTip Width="200" Style="{StaticResource ToolTip}" 
         Content="'Text Line 1' 
                   &#x0a;&#x0d;'Text Line 2' 
                   &#x0a;&#x0d;'Text Line 3'"/>

hope this helps someone else.

(The output is has the three text lines with an empty line in between each.)

1
  • you might be better off trying <ToolTip><ToolTip.Content><TextBlock Text='.....'/></ToolTip.Content></ToolTip> Nov 21, 2011 at 3:19
5

I realize this is on older question but just wanted to add that

Environment.NewLine

also works if doing this through code.

4
<TextBlock>
    Stuff on line1 <LineBreak/>
    Stuff on line2
</TextBlock>

not that it's important to know but what you specify between the TextBlock tags is called inline content and goes into the TextBlock.Inlines property which is a InlineCollection and contains items of type Inline. Subclasses of Inline are Run and LineBreak, among others. see TextBlock.Inlines

0
3

Also doesn't work with

<TextBlock><TextBlock.Text>NO USING ABOVE TECHNIQUE HERE</TextBlock.Text>

No big deal, just needed to use

<TextBlock Text="Cool &#x0a;Newline trick" />

instead.

1
<TextBox 
    Name="myTextBox" 
    TextWrapping="Wrap" 
    AcceptsReturn="True" 
    VerticalScrollBarVisibility="Visible" />
0
-1

Code behind solution

private void Button1_Click(object sender, RoutedEventArgs e)
{
    System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
    myStringBuilder.Append("Orange").AppendLine();
    myStringBuilder.Append("").AppendLine();
    myStringBuilder.Append("Apple").AppendLine();
    myStringBuilder.Append("Banana").AppendLine();
    myStringBuilder.Append("").AppendLine();
    myStringBuilder.Append("Plum").AppendLine();
    TextBox1.Text = myStringBuilder.ToString();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.