0
votes

I use a TextBlock as such:

<TextBlock Text="HelloWorld" Width="600" />

How can I have my text to render on two lines:

Hello
World

Can I use a special line wrap character, like the following?

<TextBlock Text="Hello\nWorld" Width="600" />

I do not want to change the width of the TextBlock because I do not want to use automatic text wrapping algorithm like TextWrapping="Wrap" or TextWrapping="WrapWithOverflow": I want to be able to specify in the string itself where it should wrap.

3

3 Answers

6
votes

If you use LineBreak, it should do what you want.

<TextBlock>
    Hello<LineBreak/>World
</TextBlock>

You could also use :

<TextBlock Text="Hello&#x0a;World" />
3
votes

You may set the TextBlock's Inlines property, instead of Text:

<TextBlock>
    <TextBlock.Inlines>
        <Run Text="Hello"/>
        <LineBreak/>
        <Run Text="World"/>
    </TextBlock.Inlines>
</TextBlock>

which is the explicit form of what user mcalex has answered.

2
votes

Try:

<TextBlock>
  First line
  <LineBreak />
  Second line
</TextBlock>