Multi line text in a TextBlock
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:






I have tried adding:






Re: Multi line text in a TextBlock
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:
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:
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
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:
Code: Select all
<TextBlock Text="First line
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:
Code: Select all
<TextBlock>
First line<LineBreak/>second line
</TextBlock>- Dorian
Re: Multi line text in a TextBlock
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 > <, etc.
But, when I load it from a resource, it does parse the special characters. It just seems to ignore 
.
Here's the string in a resource xaml file, which has the line break removed:
And here's the text I load from an ini file, which also doesn't work and isn't parsed:
English = Target Transfer instructions:
1. ...
In the control class, I do this in a function called from the constructor:
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 > <, etc.
But, when I load it from a resource, it does parse the special characters. It just seems to ignore 
.
Here's the string in a resource xaml file, which has the line break removed:
Code: Select all
<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:
1. ... </sys:String>
...
</ResourceDictionary>
English = Target Transfer instructions:
1. ...
In the control class, I do this in a function called from the constructor:
Code: Select all
std::string strHelpMessage = pTranslations->getTranslation("HelpText");
pTextBlock->SetText(strHelpMessage.c_str());
Re: Multi line text in a TextBlock
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
Code: Select all
<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
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