Silverlight Rich TextBlock

The Rich TextBlock control provides a read-only view of a Rich Text XML file generated by the Liquid Rich TextBox.

This Silverlight only Rich TextBlock Control is easy to implement on your Silverlight driven website and is also customizable to provide a visual feel suitable for any site design.

To use the Rich TextBlock control you will need to add a reference to Liquid.RichText.dll in your project.

How to Use the Rich TextBlock Control

To use the Rich TextBlock on your Silverlight page:

<UserControl x:Class="RichTextBlock.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:liquidRichText="clr-namespace:Liquid;assembly=Liquid.RichText"
    Width="500" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <ScrollViewer>
            <liquidRichText:RichTextBlock Width="180" RichTextURL="RichText.xml" />
        </ScrollViewer>
    </Grid>
</UserControl>
 

In this example we load an XML file containing Rich Text, this is called RichText.xml and is specified in the RichTextURL property. It is important you have this file included in your project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace RichTextBlock
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
        }
    }
}
 

Example Silverlight Rich TextBlock Control:

Silverlight Rich TextBlock Control

Your Comments and Questions Answered

 You are not logged in. You need to login to post new messages, if you do not have a login you can register for free!

yoseldvp said:

Hi,

I'm trying to load html content from a database and put it into the richTextBlock, but i ca'nt find a way to do this, does anyone know how to do this?

06/17/2009 03:49
 
lowkay said:

Is this project still active?

06/08/2009 08:01
 
lowkay said:

Nearly forgot, there's a bug that causes the rich text block to crash occassionally when copying text that contains one or more emoticons, that are matched and replaced by an image, next to each other i.e. :):D and pasting it into a rich text block.
Either the application crashes, or the smileys are inserted but have additional colons before each emoticon.
I would provide the exact error trace I get but i'm at home now and don't have access to it. I believe it's something to do with a null object reference when splitting text.

05/20/2009 01:25
 
lowkay said:

Hi,

I hope you don't think that i'm just finding bugs on purpose! Here's one that's quite strange.
When inserting xml with Insert(...):
I insert some text followed by a custom xaml control (in this case the HyperlinkButton) that is too long to fit on the same line as the preceeding text, followed by a newline and some more text.
The control and the following line are rendered onto the same row, actually i think they're different rows but they overlap.

If I then directly modify the rich text by pressing Enter it corrects itself and renders the text on the line below the control as it should be.

05/20/2009 01:12
 
lowkay said:

Howdy again,

I've come across another bug:
When inserting a link at the end of a line (i.e. nothing after the link on the same line) ctrl-clicking any point on the same line on or after the link will fire the link clicked event. This can be avoided by inserting some element after the link which bounds it correctly to just the link text.

Also is there a more efficient way to store links, as far as i'm aware you can only create a link by creating a style with a link value. For my purposes I could have many links and a style for each one is not desirable, is it possible to allow the Text element to have a link value that decides whether it is a link or not. i.e.
<Text Style="SomeStyle" Link="http://www.vectorlight.net"><![CDATA[some text]]></Text>

I'm using this control quite extensively and have sent an email to support about possible access to source code, as i'm on a tight schedule I would very much appreciate any response.

05/15/2009 02:23
 
lowkay said:

Howdy,

I have a couple of bugs to report that I've come across recently:

1.
When I use RichTextBox and call Insert(..) and provide an Image followed by a Text tag only the image is displayed, the text is lost.
i.e.
"&lt;Xaml&gt;&lt;Image Source=\"smiley.png\"/&gt;&lt;/Xaml&gt;&lt;Text Style=\"MyStyle\"&gt;![CDATA[ Hello World!]]&lt;/Text&gt;"

What's even more interesting is that any subsequent images are still displayed.

2.
When setting a text style with a left margin (something above 5 is noticeable) inserting a text element (using Insert(..) and <Text ...></Text> as a parameter) using that style, the selection brush does not take into account the margin and highlights text with an offset equal to the left margin value. i.e. it shows the selection offset to the left of the text. As an example:
this is some text
____________ < expected selection
__________    < displayed selection

It can then start selection in the middle of characters which looks bad.

05/15/2009 04:08
 
dan said:

Continued...

Sure, the RichTextBox should work fine in a Grid. It would be a great help if you could email the test project to support so I can get a handle on whats going on. Thanks!

04/21/2009 12:11
 
dan said:

Hi bwhalversen,

Yes I do. The RichText is only "calculated" at render time. What I mean by this is that you can instantiate a RichTextBlock and set its RichText property, however the RichText property is only processed (i.e. the xaml elements created) when the controls template is applied which occurs at render time.

Unfortunately this is the way it is at the moment, hopefully I will get the time to change this in a later version. Thanks!

04/21/2009 12:08
 
dan said:

Hi kdsnakeware,

I think I've answered the first 2 questions on the Rich TextBox page.

3. We did change the forum a month or so ago where all the comments were transferred to a new database which may have something to do with this.

Thanks!

04/21/2009 12:04
 
bwhalversen said:

Dan:

More on the problem below. If you put a RichTextBlock in a Grid row/column that is Auto all kinds of funky behavior occurs, For example do the following:

1. Create a new SL app.
2. In the LayoutRoot create one Auto RowDefinition and two ColumnDefinitions as follows:

ColumnDefinition0 Width=Auto
ColumnDefinition1 Width= 1, *

3. Place the RichTextBlock in column 0.
4. Set the RichTextBlock's HorizontalAlignment to Right
5. Place any other control in column 1 like a TextBox.

6. F5 run

Notice that the RichTextBlock does not display.

7. Comment out the line setting the RichTextBlock's HorizontalAlignment.
8. F5 run

Now the RichTextBlock's text displays but it arbitrarily wraps the text even though there is no newline in it and the Grid cell's width is Auto.

9. Make the text shorter like a couple words.
10, F5 run

The cell containing the RichTextBlock (whose Width is Auto) is not sizing its width according to its contents. I suspect the RichTextBlock has something to do with this since a regular TextBlock works fine in all these cases.

Can you tell me if a RichTextBlock is suppose to be usuable within a Grid and if so what are the rules on how it is sizing itself? I would be happy to send you a simple SL application that will show this odd behavior.

Brad







04/20/2009 03:48
 
bwhalversen said:

Dan:

I have created a class derived from Panel to do some custom layout. As a child of my custom Panel I am using a RichTextBlock. I create the RichTextBlock and set its RichText property to some valid XML. In my Panel's MeasureOverride() I call the Measure() method on the RichTextBlock as usual to determine its DesiredSize. This call always returns a DesiredSize with a width of 0 and a height of 2. Thus I cannot lay out the RichTextBlock correctly.

Do you have any clues as to why a RichTextBlock cannot Measure() itself?

04/16/2009 02:22
 
kdsnakeware said:

Oops, wrong forum. Will repost on RichTextBOX forum.
Plz remove my last post. Sorry...

04/06/2009 01:22
 
kdsnakeware said:

3 Questions:
1.Why doesn't the XAML output contain the XAML objects (incl tables, images)? Ironically it only contains the text.
2. Why are liquid:Bullets not nested in the XML output? This makes the to-HTML conversion so much harder.
3. Where did the history of this forum go? I've been following this and most of it seems missing. Seen this before and later it returned...

04/06/2009 01:18
 
dan said:

Thanks for reporting the problem with the Roller Blind, we'll take a look at it and fix it. Thanks!

03/21/2009 09:30
 
dan said:

Hi endquote,

These methods will only work if the RichTextBlock is loaded and rendered. Instead of Insert/InsertText you should simply set the RichText property to either XML or plain text. Thanks!

03/21/2009 09:29
 
endquote said:

Also, this "Roller Blind example" doesn't work. It fails with "AG_E_PARSER_BAD_TYPE".

03/20/2009 12:35
 
endquote said:

I'm having trouble getting started with RichTextBlock. I have an instance of it in my page, and am trying the Insert and InsertText methods to add text to it, but no text is showing up. What am I missing?

03/20/2009 12:03
 
dan said:

Hi worldvibe,

Thank you for reporting these bugs. They will be incorporated into the next version. Thanks!

02/18/2009 06:40
 
dan said:

Hi Casper,

Yes, there is a bug in the RichTextBlock where a RichTextBlock needs the IsReadOnly=true set in your C#. You cannot specify the height of a RichTextBlock as it grows to the size of the content. If you want to control the height then you should use the RichTextBox which can also be set to IsReadOnly=true also. Thanks!

02/18/2009 06:10
 
Casper van Dijk said:

Hi, can you tell me what I am doing wrong in this sample:

<UserControl x:Class="MyTestApp.Page"
     xmlns="http://schemas.microsoft.com/client/2007"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:liquid="clr-namespace:Liquid;assembly=Liquid.RichText">
     <Grid x:Name="LayoutRoot" Background="White">
         <liquid:RichTextBlock Background="Yellow" Width="180" Height="300" />
     </Grid>
</UserControl>

.. the height seems ignored, and the text block is editable (you can type in it)?!

Thanks,
Casper

02/18/2009 02:37
 
worldvibe said:

Line 5826 in RichTextBlock.cs adds a style of "Default" - however the method AddDefaultStyles does not add this style. Instead AddDefaultStyles adds a style named "Normal" stored in a variable DefaultStyle. I've changed line 5826 to use the variable DefaultStyle.

02/10/2009 11:20
 
worldvibe said:

I also found that if you move the cursor it fired an incorrect word of "doesn", I had to add the ' (tick) character to line 5673 and line 5681 in RichTextBlock.cs.

02/10/2009 08:45
 
worldvibe said:

The spell checking works very well, except when you have a word with punctuation. For example the word "doesn't" causes a failed word check. This is because the control considers "doesn" as the completed word and submits this to the SpellChecker which returns false.

I've changed RichTextBlock.cs line 709 to include the ' (tick) character. if (!Regex.Match(text, "[a-zA-Z0-9']").Success)

The control now submits the word "doesn't" to the SpellChecker which now returns true.

02/10/2009 08:28
 
dan said:

Hi sramirez,

We do an email newsletter every couple of months to keep users up-to-date with enhancements, we also have an RSS feed which you can subscribe to which is updated regularly. Thanks!

01/30/2009 11:27
 
sramirez said:

Do you have an email list that I can subscribe to or do I just need to check back from time to time?

01/28/2009 01:39
 
dan said:

Hi,

It's not possible in the current version but we are working on a data bound solution for the RichTextBlock. Thanks!

01/27/2009 07:02
 
sramirez said:

Is it possible to bind the RichTextBlock to something other then Text
<liquid:RichTextBlock Text="{Binding Note}" />

I would like to be able to bind it to the RichText but when I try that I get a Parser error.
<liquid:RichTextBlock RichText="{Binding Note}" />

01/27/2009 01:06
 
dan said:

Hi dtgreer,

In version 5.1.5 there is a DirectRichText property which as the name suggests allows you to embed RichText in the XAML like the following:

<liquid:RichTextBlock>
             <liquid:RichTextBlock.DirectRichText>
                <TextBlock>
                 &lt;LiquidRichText xmlns:liquid="clr-namespace:Liquid;assembly=Liquid"&gt;
                 &lt;Text Style="Default"&gt;&lt;![CDATA[Some Text]]&gt;&lt;/Text&gt;
                 &lt;/LiquidRichText&gt;
                </TextBlock>
             </liquid:RichTextBlock.DirectRichText>
</liquid:RichTextBlock>

It is important to XML encode anything you enter here such as the <> etc. Let us know if you need any more help with this!

01/12/2009 10:31
 
dtgreer said:

Thanks for the great controls. I know you said the binding functionality does not work in this version but is there any way for multiple users to use the Rich text box and then display it without knowing the file name? i.e. to assign it dynamically? or better yet to store the created text in a database and retrieve it from their to display.

Thanks again !

01/09/2009 02:29
 
dan said:

Hi STEVESUCH,

Thanks for your comments and were glad you like the controls. The issue you are having seems to be a bug when setting the wrap width programatically, I've added it to the fix list for the next version. Thanks!

12/26/2008 06:47
 
dan said:

Hi mmisztal,

Not at the moment, were trying to get this implemented in the next version, thanks for hilighting this!

12/26/2008 06:38
 
STEVESUCH said:

Hi,
I've downloaded your controls as I'm trying to get users to create rich text using the editor and then display using the block. Thanks a lot for providing them, they're great controls!
I'm having problems with creating the Richtextblock dynamically though. When I create them and set the wrapwidth to 400 say, they don't update and seem to stay on 180. Any ideas?

Thanks a lot.

12/22/2008 08:19
 
mmisztal said:

Is it possible to do a Text="{Bidnding SOME_PROPERTY}" in this control ? I'm trying to databind it but no luck yet

12/17/2008 08:09
 
dan said:

Hi,

Yes, remove the RichText="", the value of this property should be valid RichText XML. To set the contents of a Rich TextBlock to empty, use Text="" or just don't set the RichText field. Thanks!

12/14/2008 10:53
 
maggotbrain said:

I downloaded the richtextcontrol and try to use it.
It didn't work, I got the following exception:

AG_E_PARSER_BAD_PROPERTY_VALUE [Line: 30 Position: 23]

the tag of the control is:
                <my:RichTextBlock RichText="" WrapWidth="480" x:Name="txtPoem"></my:RichTextBlock>

What can be the problem here?

12/06/2008 05:07

Silverlight 3

Latest News

  • Silverlight 2 Controls V5.2.1 Released
    Jul, 03 2009

    After several months since the last release we have implemented many fixes to the controls library. The Rich TextBox has been improved with Links...

  • Silverlight 3 BETA Controls Released
    Mar, 30 2009

    As Silverlight 3 BETA is available now to test we thought we would present the Liquid Controls library for Silverlight 3. This BETA...

Silverlight 2 Controls

  • Rich TextBox

    Create and edit rich content with this slick and expandable Rich TextBox...

  • TreeView

    This easy to use TreeView comes with drag and drop, sorting, searching and much more...

  • Context Menu

    You too can have cool popup context menus in your Silverlight applications...

  • Resizable Dialog

    Draggable and resizable popup dialogs are what serious Silverlight developers need...

  • Spell Checker

    Real-time spell checking in Silverlight? We did it first here...