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.
I know this repeats the old question, but I had the same problem. The solution for me was to use HTML encoded strings ( &#10; ).
Line1&#10;Line2 Looks like
Line1
Line2
For more HTML encoded characters, do w3schools
Try the following:
<TextBlock> line1 <LineBreak /> line2 </TextBlock> 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 #.
<LineBreak/>
http://www.longhorncorner.com/UploadFile/mahesh/XamlLineBreak06092005152257PM/XamlLineBreak.aspx
How to split a string into two tags?
<StackPanel> <TextBlock Text="Line1" /> <TextBlock Text="Line2" /> </StackPanel> <LineBreak /> will not work if it is inside a collection, such as a Grid or StackPanel. In such cases, it will work as shown:

The correct use may be as follows:
<TextBlock> <Span>text1</Span> <LineBreak/> <Span>text2</Span> </TextBlock> 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.
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(); } } 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.)
<HyperlinkButton Content="Apply and restart this pplication! Note that modifying these settings requires the application to be restarted." /> CRLF is an easy way = !
! - Work with all wpf, xaml, silverlight controls such as TextBlock, HyperlinkText and much more.
If you bind TextBlock Text, none of the other answers work. Just add '\ n' to the anchor text to where you want to break.