WPF TextBlock Cutoff
Asked Answered
B

2

11

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.

Burberry answered 3/4, 2011 at 0:47 Comment(0)
D
20

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

Discordancy answered 3/4, 2011 at 6:24 Comment(0)
C
1

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.

Clamp answered 3/4, 2011 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.