Just a while ago, I’ve implemented a label control with a text trim behavior. If the text is longer than the available size of the label the text is truncated like this: “Text is too lo…”
Brad Cunningham and Robby Ingebretsen did a similar implementation. Both trimming the text during the measure/arrange call.
The implementation of my trimming functionality is similar to the solutions of Brad or Robby. Additionally, I’ve implemented a calculator class to get the text-width with an integrated cache. This makes the trimming function very fast.
As I saw the great examples of Brad and Robby, I decided to share my code too.
You can find the source code and a live example here:
Please note: the source code is an extract of another project and I did not change the namespace.
Here are some main decisions for the label control:
- It derived from Control, because one should not be able to add a child control to it (just like the normal TextBlock control)
- The template is hardcoded, because nobody should be able to change it.
- The text trimming functionality is implemented in the MeasureOverride method, because it’s the only place I get the available space from the parent element (including infinity)
- Each char-width of the text is calculated and cached until the text or some font property changes. This makes the calculation very fast.
- The class FontDefinitions provides an optimized char-width calculator with an integrated cache.
- The class FontDefinitions uses a TextBlock to retrieve the text length, because a TextBlock immediately returns the width via the property ActualWidth (without any layout cycle).
- The memory consumption of the font width cache is very small.


You might be interested in the following trimming algorithm, especially for Silverlight:
http://stackoverflow.com/questions/612774/right-side-ellipsis/2290045#2290045
By: dadinn on April 29, 2010
at 9:44 am
Hi,
Yes, thank you. I tried a similar algorithm as I wrote this text trimming control. Back then with Silverlight-2 I got an exception with the message “layout cycle detected”, but only in some circumstances. This was because of a limitation in the Silverlight engine how many times a control can get measured in the same layout pass. Today I’m happy that Silverlight 4 provides this functionally natively on the TextBlock control with TextTrimming=”WordEllipsis”.
Thank you for your link!
Cheers
By: beatkiener on April 29, 2010
at 10:26 am
I’m not sure the place you are getting your information, however great topic. I must spend some time learning more or understanding more. Thank you for magnificent information I was in search of this info for my mission.
By: Sherly Philbrook on June 6, 2011
at 1:13 am