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.
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 (&#10;
).
Line1&#10;Line2
Looks like
Line1
Line2
For more of the HTML encoded characters check out w3schools
Try this:
<TextBlock>
line1
<LineBreak />
line2
</TextBlock>
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#.
How about breaking the line into two tags?
<StackPanel>
<TextBlock Text="Line1" />
<TextBlock Text="Line2" />
</StackPanel>
Correct way to use it may be the following :
<TextBlock>
<Span>text1</Span>
<LineBreak/>
<Span>text2</Span>
</TextBlock>
<LineBreak/> will not work if it is inside a collection such as Grid or StackPanel. In such cases the following would work as shown:

<HyperlinkButton
Content="Apply and restart this pplication! Note that modifying these settings requires the application to be restarted." />
CRLF simple way = !
!
- Work on all wpf, xaml, silverlight controls like TextBlock, HyperlinkText and more
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.
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.
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();
}
}
This also works fine:
<TextBlock>
<Run Text="My nice text"/>
<LineBreak/>
<LineBreak/>
<Run Text="After some linebreaks, I'm back!"/>
</TextBlock>
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.)
来源:https://stackoverflow.com/questions/837367/wpf-textblock-linebreak-in-text-attribute