Silverlight RichTextEditor

I developed a RichTextEditor control for Silverlight. It inherits from the RichTextBox. It provides properties to get and set text formatting at the carat.

Here is an example of its use. The example is adapted from an official Microsoft sample TextEditor. In the MS sample formatting can be applied to selected text. In my version formatting can be applied on the fly, and the current formatting at the carat (or selection) is shown in the controls. This is more in-line with typical text editors. [The live sample link for the official sample is dead, but the download link works.]

[silverlight: silverlighttexteditor.xap, 950, 600]

Setting the text formatting at the carat was easy. The same code as the official sample worked after removing the condition to check the selection was not of zero length. To get the current formatting I used a SelectionChanged event handler on the control. It finds the attributes of the parent run of Selection.Start:

My initial approach was to use rtb.Selection.GetProperty(dependencyProperty) but this was problematic. It did not always return the expected result when the selection was empty. Accessing dependency properties is also generally slower.

The offical sample allows you to insert UIElements including images. This is for demonstration purposes only, and documents containing such elements cannot be saved. The example I’ve provided leaves these out.

Silverlight RichTextEditor project on Codeplex.

Leave a Reply

Your email address will not be published. Required fields are marked *