Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF TextBlock Cutoff

Tags:

wpf

textblock

Hi Guyz I have a WPF TextBlock of fixed width say 100 , If the string doesnt fit in the width the last character is being cutoff always as all the characters are of not the same size. I dont want to cut the character instead I want to skip the text from there and just display the text with no character cutoff.

like image 610
chetan Avatar asked Apr 03 '11 00:04

chetan


2 Answers

You have a couple of options to control wrapping and cutting of text:

  • TextWrapping can be used to make the text flow to the next line
  • TextTrimming can be used to decide how to cut text that doesn't fit

TextTrimming=None (the default) will mean that text which doesn't fit will be hidden, but it may cut down the middle of a character, which sounds like the problem you describe.

TextTrimming=WordEllipsis or TextTrimming=CharacterEllipsis will avoid showing half a character, but will append "..." to the end of the text. That will probably look better to users.

If you want to cut off the extra characters without adding the ellipsis, you'd have to use the technique Ed S. described

like image 108
Paul Stovell Avatar answered Nov 07 '22 20:11

Paul Stovell


I suppose that I don't really understand your use case here. My first suggestion would be to simply dynamically size your TextBlock. If that's not possible then you wil have to get the width of the string and manipulate it yourself before you set it in the TextBlock (or use a fixed width font assuming that you can and you know the max length of the string).

If you need to measure the width of the string before it is displayed you can use the FormattedText class to do so.

like image 36
Ed S. Avatar answered Nov 07 '22 21:11

Ed S.