Reputation: 127
I have Multiline TextBlock, I want to get all it's lines by code Can someone help me?
The TextBlock:
<TextBlock Name="tb" TextWrapping="Wrap" >
Name:_____________
<LineBreak/>
Mark:____________
</TextBlock>
In C#:
text = ((TextBlock)tb).Text;
But I got only the first line.
Thanks!
Upvotes: 4
Views: 12724
Reputation: 717
Here it show 3 possible ways of getting this done. Please use which suits your requirement.
1.<LineBreak />
2.TextWrapping="Wrap"
3.TextTrimming="CharacterEllipsis"
Upvotes: 1
Reputation: 1935
If you want to display on multiple lines you can use :
<TextBlock Name="myText" Text="I go 
 Home " >
and sure, you can get all lines by parsing the string.
Upvotes: 1
Reputation: 17580
You can try this:
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 it here
Upvotes: 4