Silverlight 4 - Trim Text in a TextBlock and display Ellipses instead

What happens when you have text that does not fit its container? Let’s see.

    <Grid x:Name="LayoutRoot" Background="White" Margin="40">
<
TextBlock Name="tb" Height="20" HorizontalAlignment="Left"
Text="This sentence is too long to fit in here"
Width="100">
</
Grid>

If you run the application, you will observe that the text gets chopped off when it overflows the edge of its container.

TextTrimming Silverlight

To resolve this issue, you could either use the TextWrapping option to wrap the text or instead visually depict that the text is too big for its container. We usually depict this using the trailing three ellipsis(…)

Silverlight 4 introduces the TextTrimming property which adds an ellipses(…) when the text overflows the edge of its container. Here’s how to use this property.

    <Grid x:Name="LayoutRoot" Background="White" Margin="40">
<
TextBlock Name="tb" Height="20" HorizontalAlignment="Left"
Text="This sentence is too long to fit in here"
Width="100" TextTrimming="WordEllipsis"/>
</
Grid>

Now running the same piece of code with the TextTrimming property will produce this output with the trailing ellipses

TextTrimming Silverlight

Note: TextTrimming property was available in WPF.






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Beben Koben said...

hai mate...your article so usefully
may be you think for submit your article to here...
http://scriptandstyle.com/
theres so many article from famous web...may be you want to think my idea...xixixi ;))
for promote my brother