Click here to Skip to main content
13,448,549 members (57,232 online)
Click here to Skip to main content
Add your own
alternative version


Posted 30 Jun 2010

Text Trimming in Silverlight 4

, 30 Jun 2010
Rate this:
Please Sign up or sign in to vote.
Here I will show you how you can use the Text Trimming functionality in Silverlight 4.

Have you ever tried trimming your Text in Silverlight 2 or Silverlight 3? If yes, just recall the lines of code you wrote for trimming your text content and showing an Ellipsis (i.e., "..." three dots) at the end of the text. If you didn’t try it earlier, then just imagine what you have to do and how you will do it. Also imagine the number of lines you have to write. Confused smile

In this post, I will show you how I can implement this feature the easy way. Stop!!! I will not write a huge code here nor will I use any library to do that. Microsoft has added this functionality in Silverlight 4. You just have to set the Enum value to the TextBlock property. Surprised smile Wao!!!

So, how to do that? Let us try it.

Create a new Silverlight Project and add one TextBox and a TextBlock. I will bind the TextBox content to the TextBlock so that when we modify the content of the TextBox, it will immediately reflect in the TextBlock’s Text content.

You can find the code here:

<TextBox x:Name="txtMessage" Width="200" Height="25" Margin="10" />
<TextBlock x:Name="txbMessage" Text="{Binding Path=Text, ElementName=txtMessage}" 

TextWrapping="Wrap" Width="200" Height="60" Margin="10" />

Be sure that you set some boundary to the TextBlock, i.e., Height and Width. It will make the TextBlock a fixed size control. Once you run your application, start typing on the TextBox and you will notice that the TextBlock itself is updating with the text you are entering in the TextBox automatically. If you are writing a huge amount of text inside the TextBox, you will notice that after the specified size, the text inside the TextBlock is growing but not creating any Ellipsis!!! Confused smile


I think you are confused again!!! Why is it not working!!! Wait a minute. We didn’t instruct the TextBlock to trim the text. Now let us do that. We will set the enum property “TextTrimming” to “WordEllipsis” and here is the code for the same:

<TextBox x:Name="txtMessage" 

Width="200" Height="25" Margin="10" />
<TextBlock x:Name="txbMessage" 

Text="{Binding Path=Text, ElementName=txtMessage}" 

TextTrimming="WordEllipsis" TextWrapping="Wrap" 

Width="200" Height="60" Margin="10" />

Once you run your application now and start typing a huge content, you will see that after a certain length of text (generally the dimension of the TextBlock to set the content), the whole string has been cropped and set one Ellipsis, i.e., three dots ("...") at the end of the last word. If you type more inside the TextBox, it will not reflect in the TextBlock content.


This is a new feature in Silverlight 4 and you will find it very useful when you want to trim some portion of text. You don’t have to write any code for it to implement. It is available by default. So, why wait? Go and try the sample code. Enjoy... Smile


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...

Comments and Discussions

GeneralReason for my vote of 5 Well Explained Pin
Member 747486028-Sep-10 18:06
memberMember 747486028-Sep-10 18:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180318.3 | Last Updated 30 Jun 2010
Article Copyright 2010 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid