mich
mich

Reputation: 127

Get all text in multiline wpf textblock

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

Answers (3)

Kamlendra Sharma
Kamlendra Sharma

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"

here

Upvotes: 1

Dragos Stoica
Dragos Stoica

Reputation: 1935

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

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

and sure, you can get all lines by parsing the string.

Upvotes: 1

Johan Larsson
Johan Larsson

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

Related Questions