tags:

views:

431

answers:

2

Hi folks, I just received a requirement to display a length of text in a control. The control is of a particular width and will be up to 2 lines in height. If it renders longer than two lines it will just display "..." at the end of the string.

Is this possible with any of the stock standard WPF controls?

Thanks, D.

A: 

That's not standard behavior that I've ever found, but again I've not looked for it.

One possibility is to use a monospace font in a TextArea control, and then if the string is greater than however many characters fit in the area, only display the right N characters with the ellipses

Stephen Wrighton
+5  A: 

Set the Height of the TextBlock to be high enough to fit two lines. Set the TextWrapping to Wrap and the TextTrimming to CharacterEllipsis or WordEllipsis.

For the default Segoe UI 12Pt font, I find this does it

<TextBlock TextWrapping="Wrap" Height="40" TextTrimming="CharacterEllipsis" />

You could probably do some code behind to work out the height it should be be for a particular font if you want.

Ray