Skip to content


RichTextBox in Silverlight 4

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

I was trying my hands on Silverlight 4 Beta version and found this is wonderful control – RichTextBox.. This was much needed on Web Platform and Microsoft has really made it work simple… Rather, thanks to the invention of XAML!

This small control supports many features. Some of them are:

  • Formatting of text at runtime – font, color, bold, italics, underline, etc.
  • Addition of inline UI Elements in the Text Box itself.
  • Read / Write only modes both supported.
  • Hyperlinks can also be inserted in the text.

Let’s do some scripting. Two ways to work:-

Way 1: Texting through XAML

</div>
<div id="_mcePaste" style="text-decoration: underline; position: absolute; left: -10000px; top: 142px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><RichTextArea VerticalScrollBarVisibility="Auto" x:Name="richTextBox"></div>
<div id="_mcePaste" style="text-decoration: underline; position: absolute; left: -10000px; top: 142px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><Paragraph> This is a <Bold>sample</Bold> text. </Paragraph></div>
<div id="_mcePaste" style="text-decoration: underline; position: absolute; left: -10000px; top: 142px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"></RichTextArea></div>
<div id="_mcePaste" style="text-decoration: underline; position: absolute; left: -10000px; top: 142px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">

<RichTextArea VerticalScrollBarVisibility="Auto" x:Name="richTextBox">

<Paragraph> This is a <Bold>sample</Bold> text. </Paragraph>

</RichTextArea>

Way 2: Texting through C# Code


// Create a Run of plain text and some bold text.

Run myRun1 = new Run();

myRun1.Text = "This is a ";

Bold myBold = new Bold();

myBold.Inlines.Add("sample");

Run myRun2 = new Run();

myRun2.Text = "text.";

// Create a paragraph and add the Run and Bold to it.

Paragraph myParagraph = new Paragraph();

myParagraph.Inlines.Add(myRun1);

myParagraph.Inlines.Add(myBold);

myParagraph.Inlines.Add(myRun2);

// Add the paragraph to the RichTextArea.

richTextBox.Blocks.Add(myParagraph);

//Add the RichTextArea to the StackPanel.

stackPanel.Children.Add(richTextBox);

Hope this helps!


Related Posts:

Posted in C#, Silverlight.

Tagged with .


One Response

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Continuing the Discussion

  1. RichTextBox in Silverlight 4 | ganshani.com iSilverlight linked to this post on December 24, 2009

    [...] post: RichTextBox in Silverlight 4 | ganshani.com By admin | category: silverlight 2 | tags: linux, microsoft, moonlight, novell-linux, [...]



Some HTML is OK

or, reply to this post via trackback.