The richness of the Silverlight Textblock

29 01 2010

With Silverlight 4, we saw the introduction of the RichTextBox control. Before using the RichTextBox I had been stacking multiple TextBlock controls on top of each other to create “richness” in my text.

I’m not writing today about the RichTextBox control, but the common TextBlock control and how rich it can be. These are two simple tips to creating rich text hassle free in Silverlight.


1. <Run>

By using this tag, I can insert any stylechange mid sentance in a Textblock control. Let me give an example from the webshop I’ve been working on:

<TextBlock Style="{StaticResource Text}">
   We do <Run FontWeight="Bold">not</Run> accept returns without proper postage or as “collect mail”.

This will show like this on the page:

We do not accept returns without…”

You can use the <Run> tag to change size, color, font, etc. in a quick and easy way mid-text.

2. <Linebreak/>

Inserting a <Linebreak/> anywhere in a textblock is the same as a “\n” or a <br/> in other situations. It’s very handy when writing in XAML and has enabled me to save a lot of space.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: