Click here to Skip to main content
Licence CPOL
First Posted 9 May 2011
Views 6,806
Bookmarked 2 times

Tips: Formatting Silverlight TextBlock Control

In general we use multiple TextBlock controls inside a StackPanel to format a single line. In this post I will show you a different way to format a single line. This is very useful if you want to bind any data in the middle of the text string. Also this is useful if you want to format the line with
A Technical Blog article. View original blog here.[^]

In general we use multiple TextBlock controls inside a StackPanel to format a single line. In this post I will show you a different way to format a single line. This is very useful if you want to bind any data in the middle of the text string. Also this is useful if you want to format the line with multiple formatting option. Read to know more about it.

In general, if we want to bind a data in between text or want to style a single line with multiple text format, how we design it? We use StackPanel with multiple TextBlock wrapped using proper orientation as shown below:

<StackPanel Orientation="Horizontal" DataContext="{StaticResource User}">
    <TextBlock Text="Welcome "/>
    <TextBlock Text="{Binding Name}"/>
    <TextBlock Text=" to our site."/>
</StackPanel>

 

Now, in this Tip I will show you a different option to do the same thing in more proficient way. Instead of using multiple TextBlock control, we will use a single TextBlock control now and in this case, we will not need any StackPanel too.

So, how to implement it? Add a TextBlock control in your XAML page and insert <Run /> tag to insert multiple text chunks. All the text formatting properties like FontFamily, FontSize, Foreground color etc. are also part of the <Run /> tag.

In the below code snippet, I demonstrated the use of "Run" to insert a DataBinding at the middle of the string to add the User's name:

<TextBlock DataContext="{StaticResource User}">
    <Run Text="Welcome"/>
    <Run Text="{Binding Name}"/>
    <Run Text="to our site"/>
</TextBlock>

 

What's more? There are more other option's too. You can directly use <Bold />, <Italic />, <Underline /> tags to format the text in proper fashion. If you want to add a single line break, you can do so by using the <LineBreak /> tag. If you don't use the LineBreak tag, the texts will place in a single line. Also, if you want to add more formatting to your text, you can use the <Span /> tag which has a set of properties to control your styling.

Check the below code for more details:

<TextBlock>
    <Bold>This is a Bold Text.</Bold>
    <Italic>It's an Italic Text.</Italic>
    <Underline>This Text has Underline.</Underline>
    <LineBreak/>
    <Span Foreground="Red">Spaned text with Color Red.</Span>
</TextBlock>

 

The above code will output the following text in the UI:

image

 

Hope, this tips will help you next time while working with text formatting in Silverlight. There are many option there to customize the string style. Explore it more and who knows, it will help you in future.

License

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

About the Author

_ Kunal Chowdhury _

Software Developer

India India

Member

Follow on Twitter Follow on Twitter
Kunal Chowdhury is a Microsoft MVP (Most Valuable Professional) in Silverlight Technology, a Codeproject MVP & Mentor, DZone MVB (Most Valuable Blogger), Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working as a Software Engineer II in an MNC located at Pune, India. He has a very good skill over XAML, C#, Silverlight and WPF. He has a good working experience in Windows 7 application (including Multi-touch) development too.
 
He posts his findings in his technical blog. He also writes for SilverlightShow and Codeproject portal. Many of his articles were highlighted as "Article of the Day" in Microsoft sites.
 
He also has another website called Silverlight-Zone.com where he posts article links on Silverlight, Windows Phone 7 and XNA accumulated from various web sites to help the community grow on specified technologies.
 
You can reach him in his Blog : http://www.kunal-chowdhury.com
He is also available in Twitter : http://twitter.com/kunal2383

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 Pinmemberjanslodicka3:21 30 May '11  
GeneralRe: My vote of 3 PinmvpKunal_Chowdhury3:39 30 May '11  
GeneralRe: My vote of 3 Pinmemberjanslodicka4:20 30 May '11  
GeneralJoking? Pinmemberjanslodicka3:01 30 May '11  
GeneralRe: Joking? PinmvpKunal_Chowdhury3:22 30 May '11  
GeneralRe: Joking? Pinmemberjanslodicka4:28 30 May '11  
GeneralMy vote of 5 PinmvpBrij3:21 9 May '11  
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury3:41 9 May '11  
GeneralMy vote of 5 PinmvpAbhishek Sur3:20 9 May '11  
GeneralRe: My vote of 5 PinmvpKunal_Chowdhury3:37 9 May '11  
GeneralNice job PinmemberShahriar Iqbal Chowdhury3:05 9 May '11  
GeneralRe: Nice job PinmvpKunal_Chowdhury3:12 9 May '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 9 May 2011
Article Copyright 2011 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid