cdoty
Topic Author
Posts: 15
Joined: 30 Nov 2017, 13:34

Multi line text in a TextBlock

31 Jul 2026, 01:00

I am trying to add line breaks in a TextBlock Text property. I need to use the Text property for binding.

I have tried adding:








 
User avatar
dstewart
Site Admin
Posts: 133
Joined: 22 Jan 2024, 11:41

Re: Multi line text in a TextBlock

31 Jul 2026, 15:12

Hi cdoty! Dorian here!

There are three different techniques through which you can insert line breaks:

In Noesis Studio (Method A1)

With a TextBlock Element selected, in the 'Text' Property's field, pressing [Shift (Hold) + Enter] will generate a line break.


Composing XAML via a Text Editor (Method B1)

The 
 character will generate a LineBreak, as in the following example:
<TextBlock Text="First line&#x0a;second line" />

Composing XAML via a Text Editor (Method B2)

You can also use the <LineBreak> tag within a TextBlock to achieve this, as in the following snippet:
<TextBlock>
    First line<LineBreak/>second line
</TextBlock>
Let us know if does the trick for you, and if there's anything else we can assist with. If we don't catch each other again before end of day, have a great weekend!

- Dorian
 
cdoty
Topic Author
Posts: 15
Joined: 30 Nov 2017, 13:34

Re: Multi line text in a TextBlock

31 Jul 2026, 18:33

If I paste the text directly into the xaml.
But, if I pull in the text through binding to a resource xaml or set in on initialization, it doesn't.
When I set it on initialization, it doesn't parse any of the special characters, such as &gt; &lt;, etc.
But, when I load it from a resource, it does parse the special characters. It just seems to ignore &#x0a;.

Here's the string in a resource xaml file, which has the line break removed:
<ResourceDictionary
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:sys="clr-namespace:System;assembly=mscorlib">
	...
    <sys:String x:Key="HelpText">Target Transfer instructions:&#x0a;1.  ... </sys:String>
    ...
</ResourceDictionary>
And here's the text I load from an ini file, which also doesn't work and isn't parsed:
English = Target Transfer instructions:&#x0a;1. ...

In the control class, I do this in a function called from the constructor:
std::string	strHelpMessage	= pTranslations->getTranslation("HelpText");

pTextBlock->SetText(strHelpMessage.c_str());
 
User avatar
jsantos
Site Admin
Posts: 4822
Joined: 20 Jan 2012, 17:18
Contact:

Re: Multi line text in a TextBlock

31 Jul 2026, 23:32

Because of how XML parsers handle whitespace, physical line breaks inside tags are usually ignored. If you want to format the text over multiple lines without using special character codes, you need to add the xml:space="preserve" attribute to the tag. This tells the parser to respect your actual line breaks
<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">
  
  <Page.Resources>
    <sys:String x:Key="HelpText" xml:space="preserve">First line&#x0a;second line</sys:String>
  </Page.Resources>
  
  <Grid>
    <TextBlock Text="{StaticResource HelpText}" />
  </Grid>
</Page>

Who is online

Users browsing this forum: No registered users and 13 guests