LineBreak in TextBlock

19 Oct

If you need a LineBreak in a TextBlock you have some options depending on your scenario.

In XAML

If you enter the text in XAML you can add a LineBreak tag to your text.

<TextBlock>
    <Run>Lorem Ipsum has been the industry's standard</Run>
    <LineBreak />
    <Run>It has survived not only five centuries.</Run>
</TextBlock>

In C#

If you enter the text in C# you can add \n to your text to add a new line.

TextBlock1.Text = 
  "Lorem Ipsum has\nIt has survived not only.";

where TextBlock1 is my TextBlock.

In Binding

If you get your text from a database and use Binding to add text to your TextBlock neither of the two solutions above will work. As long as it is text being bound to the Text property of the TextBlock, then all you need to do is embed CR/LF character codes into your data.

by xamlgeek

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: