WPF Textblock, linebreak in Text attribute

WpfTextblock

Wpf Problem Overview


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.

Wpf Solutions


Solution 1 - Wpf

Try this:

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

Solution 2 - Wpf

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

Solution 3 - Wpf

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

Solution 4 - Wpf

Solution 5 - Wpf

<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

Solution 6 - Wpf

How about breaking the line into two tags?

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

Solution 7 - Wpf

Correct way to use it may be the following :

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

Solution 8 - Wpf

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.

Solution 9 - Wpf

  <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

Solution 10 - Wpf

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.

Solution 11 - Wpf

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

Solution 12 - Wpf

This also works fine:

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

Solution 13 - Wpf

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

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

Solution 14 - Wpf

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.

Solution 15 - Wpf

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

Solution 16 - Wpf

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionScottCateView Question on Stackoverflow
Solution 1 - WpfPaul AlexanderView Answer on Stackoverflow
Solution 2 - WpfNoakiView Answer on Stackoverflow
Solution 3 - WpfStephane HalimiView Answer on Stackoverflow
Solution 4 - WpfjcollumView Answer on Stackoverflow
Solution 5 - Wpfuser2063329View Answer on Stackoverflow
Solution 6 - WpfCameron MacFarlandView Answer on Stackoverflow
Solution 7 - Wpfradu florescuView Answer on Stackoverflow
Solution 8 - WpfErisvaldo JuniorView Answer on Stackoverflow
Solution 9 - Wpfuser1551704View Answer on Stackoverflow
Solution 10 - WpfnewmanView Answer on Stackoverflow
Solution 11 - WpfDanView Answer on Stackoverflow
Solution 12 - WpfChristoffer ErikssonView Answer on Stackoverflow
Solution 13 - WpfWelcorView Answer on Stackoverflow
Solution 14 - WpfMelloGView Answer on Stackoverflow
Solution 15 - WpfJodrellView Answer on Stackoverflow
Solution 16 - WpfMAYUR GOWDA M NView Answer on Stackoverflow