WPF Textblock, linebreak in Text attribute
Asked Answered
H

16

111

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.

Hardej answered 7/5, 2009 at 22:22 Comment(2)
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";Chromoprotein
&#10; works fine in xaml. Thank you JJ_Coder4Hire !Postimpressionism
E
124

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

Ency answered 28/10, 2009 at 16:5 Comment(3)
@Knasterbax if the text is specified as the content between <TextBlock> and </TextBlock> then yeah, you'll need <LineBreak/>. &#10; works inside the Text attribute: <TextBlock Text="..." />Baghdad
"&#10;" worked for me. I did not need the "&amp;" as mentioned though. This method will also work when setting the text like so: <Setter Property="Text" Value="Line 1&#10;&#10;Line 2" />Bolin
The "outside source" should rather use Environment.NewLine.Parolee
L
138

Try this:

<TextBlock>
    line1
    <LineBreak />
    line2
</TextBlock>
Loram answered 7/5, 2009 at 22:24 Comment(2)
Haven't tried this directly but off the top of my head you could try xml:space="preserve" and then embed the line break in the Text property directly.Loram
Capitalization matters. It's <LineBreak />.Mazy
E
124

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

Ency answered 28/10, 2009 at 16:5 Comment(3)
@Knasterbax if the text is specified as the content between <TextBlock> and </TextBlock> then yeah, you'll need <LineBreak/>. &#10; works inside the Text attribute: <TextBlock Text="..." />Baghdad
"&#10;" worked for me. I did not need the "&amp;" as mentioned though. This method will also work when setting the text like so: <Setter Property="Text" Value="Line 1&#10;&#10;Line 2" />Bolin
The "outside source" should rather use Environment.NewLine.Parolee
D
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#.

Delay answered 9/1, 2013 at 3:33 Comment(1)
Duplicate answer to Paul Alexander's.Conventioneer
B
8

<LineBreak/>

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

Buerger answered 7/5, 2009 at 22:23 Comment(3)
Scott I don't understand your comment.Buerger
The problem is that <LineBreak/> 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.Tyrolienne
@Tyrolienne 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.Parolee
L
7

How about breaking the line into two tags?

<StackPanel>
    <TextBlock Text="Line1" />
    <TextBlock Text="Line2" />
</StackPanel>
Leviticus answered 8/5, 2009 at 0:13 Comment(0)
S
7

Correct way to use it may be the following :

<TextBlock>  
    <Span>text1</Span>  
    <LineBreak/>  
    <Span>text2</Span>  
</TextBlock>
Somebody answered 10/4, 2012 at 14:15 Comment(2)
Aside from the fact that that doesn't answer the question (outside source) - what's "more correct" with Span than without?Parolee
@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.Somebody
Q
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

Quintet answered 29/5, 2014 at 22:12 Comment(3)
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.Dorothadorothea
@Dorothadorothea 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?Parolee
@TheincredibleJan It has been 4 years, so no, I don't have the code. It probably looked something like this: <RadioButton><StackPanel><TextBlock>Line 1</TextBlock><TextBlock>Line 2</TextBlock><TextBlock>Line 3</TextBlock></StackPanel></RadioButton>. 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 settingsDorothadorothea
A
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.

Aidoneus answered 13/1, 2021 at 22:35 Comment(1)
Just a duplicate...Parolee
A
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

Acrobatics answered 31/8, 2012 at 8:24 Comment(1)
(go fix your other answer and flag for it to be undeleted)Extravagate
H
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.

Henceforward answered 10/3, 2013 at 20:43 Comment(0)
K
5

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

$"first line {Environment.NewLine} second line"
Karttikeya answered 25/6, 2020 at 15:34 Comment(0)
N
4

This also works fine:

<TextBlock>
    <Run Text="My nice text"/>
    <LineBreak/>
    <LineBreak/>
    <Run Text="After some linebreaks, I'm back!"/>
</TextBlock>
Naif answered 8/2, 2019 at 14:38 Comment(0)
H
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();
    }
}
Highhat answered 26/3, 2014 at 6:46 Comment(0)
I
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.)

Impound answered 8/4, 2011 at 9:23 Comment(0)
S
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.

Staffer answered 4/12, 2012 at 12:58 Comment(0)
M
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>
Mennonite answered 27/9, 2021 at 5:24 Comment(1)
"outside source"Parolee

© 2022 - 2024 — McMap. All rights reserved.