What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10787 |  Welcome, Guest!   Register  Login
Home > Articles > Silverlight > Text Trimming in Silverlight 4

Text Trimming in Silverlight 4

1 vote(s)
Rating: 4 out of 5
Article posted by Kunal2383 on 7/2/2010 | Views: 6093 | Category: Silverlight | Level: Beginner red flag


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. Also imagine the no. of lines you have to write.

Here I will describe you Step-by-Step on the String Trimming functionality in Silverlight 4.

Download


 Download source code for Text Trimming in Silverlight 4


Introduction

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. Also imagine the no. 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 here a huge code nor I will 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.

Begin with the Code

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 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

 

image

 

I think, you are confused again!!! Why it is 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.

 

image

End Note

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 to wait for? Go and try the sample code. Enjoy… Smile

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.kunal-chowdhury.com
Member since:Monday, March 01, 2010
Level:Starter
Status: [Member]
Biography:He is currently working as a Silverlight application developer. Has a very good skill over C#, XAML, Silverlight & WPF. He has a good working experience in Windows 7 application (including Multitouch) development.

During his professional career he worked in various technologies & delivered quality output. He never hesitates to take up challenges & work on the latest technologies in Microsoft platform.

He attended various software development competition & achieved different awards.

He is presently focusing on the RIA (Silverlight & WPF) & willing to become a Technology Specialist in Microsoft platform. Learning newer things, Blog posting & helping others in forums is one of his regular activity.

Specialties: Silverlight Application Development, WPF Application Development, Windows 7 Application Development
>> Write Response - Respond to this post and get points
Related Posts

Silverlight 4 now came up with the support of Command Binding. Using Command binding you can easily develop your Silverlight MVVM (Model-View-ViewModel) applications where your view will not know about data. In this article, I will describe you the Command binding feature in Silverlight 4 Step-by-Step. In this example, I will create a Customer View which will load the customer information from the code behind once we click the button “Load Customers”. The button will have a command associated with it to load the records instead of loading on click event. This ensures that the view will have less code & no relation with the data.

It has been a while since I have written an article. This is because of the load of work that I am doing at my job, privately and here in DNF. Since well I have free time, I will dedicate this time to write new and finish the series that I have started and never finished. This is a small article that i use to demonstrate to you how to play a video in Silverlight.

In this article, we are going to learn about Different types of Data Binding Techniques in Silverlight and how to validate Data in SilverLight.

In this Article we are going to learn Silverlight controls like Grid and TextBlock and related properties.

In this article we will look as how to invoke Silver light methods from javascript

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 1:14:27 AM