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.



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

Beben 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