WPF TextBlock Cutoff

Hi Guyz I have a WPF text block with a fixed width, say 100. If the line does not fit the width, the last character is always truncated, since all characters are the same size. I don’t want to cut the character instead, I want to skip the text from there and just display the text without trimming the characters.

+7
source share
2 answers

You have several options for managing text wrap and cut:

  • TextWrapping can be used to translate a text stream to the next line.
  • TextTrimming can be used to decide how to cut text that doesn't fit.

TextTrimming=None (by default) will mean that text that doesn't fit will be hidden, but it can cut out the middle of the character, which sounds like the problem you are describing.

TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid displaying half the character, but will add "..." to the end of the text. It will probably look better for users.

If you want to disable extra characters without adding an ellipsis, you will have to use the Ed S. technique described

+13
source

I suggest that I really do not understand your use case here. My first suggestion was to simply dynamically resize your TextBlock. If this is not possible, you will need to get the line width and manipulate it yourself before setting it in a TextBlock (or use a fixed-width font, assuming you can, and you know the maximum line length).

If you need to measure the width of a string before displaying it, you can use the FormattedText class to do this.

+1
source

All Articles