Or is...">

WPF Textblock, linebreak in Text attribute

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

 <TextBlock Text="line1\nLine2" /> 

Or is there a better way to force an average line break inside a Text attribute?

 <LineBreak /> 

This does not work for me, it should be the value of the Text attribute, because the text string is set from an external source.

I am familiar with LineBreak , but this is not the answer I'm looking for.

+74
wpf textblock
May 07 '09 at 10:22
source share
12 answers

I know this repeats the old question, but I had the same problem. The solution for me was to use HTML encoded strings ( &amp;#10; ).

 Line1&amp;#10;Line2 

Looks like

Line1
Line2

For more HTML encoded characters, do w3schools

+98
Oct 28 '09 at 16:05
source share

Try the following:

 <TextBlock> line1 <LineBreak /> line2 </TextBlock> 
+97
May 7, '09 at 22:24
source share

The easiest way -

<TextBlock> blabla <LineBreak /> coucou <LineBreak /> coucou 2 </TextBlock>

So you just write the XAML code, and <LineBreak /> has the same meaning as in HTML or "\ n" in C #.

+10
Jan 09 '13 at 3:33
source share
+6
May 7, '09 at 22:23
source share

How to split a string into two tags?

 <StackPanel> <TextBlock Text="Line1" /> <TextBlock Text="Line2" /> </StackPanel> 
+5
May 08 '09 at 12:13 a.m.
source share

<LineBreak /> will not work if it is inside a collection, such as a Grid or StackPanel. In such cases, it will work as shown:

LineBreak inside a collection

+2
May 29 '14 at 10:12
source share

The correct use may be as follows:

 <TextBlock> <Span>text1</Span> <LineBreak/> <Span>text2</Span> </TextBlock> 
+1
Apr 10 2018-12-12T00:
source share

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

eg.

Mine is working fine. In addition, you have no problem changing the text dynamically.

+1
Dec 04
source share

I'm late to the party, but .. it's more or less how I did it (the mind of my ItemSources is simple strings, not formatted, and I didn't need to β€œconvert” 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
Mar 26 '14 at 6:46
source share

I had a similar problem and wanted to associate a xaml markup String with a TextBlock. Essentialy saving declarative markup inside a TextBlock in a string for later use.

Here's how I did it : I subclassed TextBlock to make InlineCollection connected and wrote a converter between the line and InlineCollection (or actually a general Inlines list.)

0
Apr 08 2018-11-11T00:
source share
  <HyperlinkButton Content="Apply and restart this pplication!&#10;&#13;Note that modifying these settings requires the application to be restarted." /> 

CRLF is an easy way = !&#10;&#13;

!&#10;&#13; - Work with all wpf, xaml, silverlight controls such as TextBlock, HyperlinkText and much more.

0
Aug 31 '12 at 8:24
source share

If you bind TextBlock Text, none of the other answers work. Just add '\ n' to the anchor text to where you want to break.

0
Mar 10 '13 at 20:43
source share



All Articles