2

Is there any way to get TextBlock lines as separate strings after text wrapping? For example, if I have a TextBlock defined like this:

TextBlock myTextBlock = new TextBlock();
myTextBlock.TextWrapping = TextWrapping.Wrap;
myTextBlock.Text = veryLongString;

Is it possible to get separate string for each row that is displayed in my TextBlock, which is created by TextWrapping.Wrap?

Rodia
  • 1,407
  • 8
  • 22
  • 29
Stojdza
  • 445
  • 2
  • 10
  • 32
  • There's a reasonable looking implementation of a TextBox styled to behave like a TextBlock here: http://stackoverflow.com/questions/1105982/visible-line-count-of-a-textblock, providing access to the LineCount property. Could be a practical approach. – goobering Apr 23 '15 at 11:41

1 Answers1

0

TextBlock has property called Inline, you could use that and access the strings one by one,

            StringBuilder s = new StringBuilder();
            foreach (var line in txtsample.Inlines)
            {
                if (line is LineBreak)
                    s.AppendLine();
                else if (line is Run)
                    s.Append(((Run)line).Text);
            }
            var text = s.ToString();
           }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • I tried this but the probelm is that I always have only one line, which has the value of the whole "veryLongString" string, so I don't loop through InlinesCollection becouse it only have one value. – Stojdza Apr 23 '15 at 09:35
  • you should have linebrakes inorder to get the lines, otherwise try textbox which has lines property – Sajeetharan Apr 23 '15 at 09:37
  • Even if you set `Text` to a string with line breaks, the whole text goes into a single Run, which will be the only element of the `Inlines` collection. – Clemens Apr 23 '15 at 10:48