Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I truncate a string with an ellipsis in a Silverlight TextBlock?

If I display a string too long for a TextBlock it just appears to keep writing past the edge of the TextBlock. I'd rather it use the common technique of adding an ellipsis ("...") if the text is not going to fit in the space provided. How should I go about doing this in Silverlight?

The references I've found all use the TextRenderer class which is not available in Silverlight

like image 285
dlanod Avatar asked Sep 07 '11 00:09

dlanod


2 Answers

You didn't say which Silverlight version this is. Assuming Silverlight 4, it's baked into TextBlock via the TextTrimming property.

<TextBlock TextTrimming="WordEllipsis"/>

The only thing you have to do is make sure your TextBlock's width is properly restricted and you use NoWrap for TextWrapping.

like image 50
Adam Sills Avatar answered Sep 19 '22 04:09

Adam Sills


Set the TextTrimming property to WordEllipsis.

<TextBlock Text="My long text" TextTrimming="WordEllipsis"/>
like image 24
NotDan Avatar answered Sep 20 '22 04:09

NotDan