Get all text in wpf multiline text block

I have a Multiline TextBlock, I want to get all the lines by code. Can anyone help me?

Text block:

<TextBlock Name="tb" TextWrapping="Wrap" > Name:_____________ <LineBreak/> Mark:____________ </TextBlock> 

In C #:

 text = ((TextBlock)tb).Text; 

But I got only the first line.

Thanks!

+4
source share
3 answers

You can try the following:

 StringBuilder s = new StringBuilder(); foreach (var line in tb.Inlines) { if (line is LineBreak) s.AppendLine(); else if (line is Run) s.Append(((Run) line).Text); } var text = s.ToString(); 

Found here

+5
source

If you want to display on multiple lines, you can use:

 <TextBlock Name="myText" Text="I go &#x0a; Home " > 

and of course you can get all the lines by parsing the line.

+1
source

Here are 3 possible ways to do this. Please use that meets your requirements.

 1.<LineBreak /> 2.TextWrapping="Wrap" 3.TextTrimming="CharacterEllipsis" 

here

0
source

All Articles