Search code examples
wpfword-wraptextblock

In WPF, can the word-wrapping of a TextBlock use the text of inline Runs to generate breaks between words instead of in their middles?


In WPF, if you have inline Runs inside of a text block, word-wrapping works on a per-letter basis. I'd like it to use the default functionality that is word-by-word wrapping, i.e:

lorem
ipsum

instead of

lorem ip
sum

I'm generating the inline Runs programmatically, code snippet as follows:

TextBlock tb = new TextBlock() {
    TextWrapping = TextWrapping.Wrap
};

foreach (string part in parts)
{
    tb.Inlines.Add(new Run(part));
}

The Runs are woven inside the TextBlock with InlineUIContainers that contain Images. If I set the Text property of the Textblock instead of using Runs it works normally, but I can't do that and weave in images with the text. Very open to alternative solutions as well.


Solution

  • erotavlas suggested I use tb.Inlines.Add(part) instead and it works precisely as requested.