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.

writing

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”.
</TextBlock>

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.

Advertisements

Actions

Information

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s




%d bloggers like this: