Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tips: Formatting Silverlight TextBlock Control

0.00/5 (No votes)
9 May 2011 1  
How to format 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 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 do 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 a more proficient way. Instead of using a 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 other options too. You can directly use <Bold />, <Italic />, <Underline /> tags to format the text in a 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 tip will help you next time while working with text formatting in Silverlight. There are many options there to customize the string style. Explore it more and who knows, it will help you in future.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here