views:

1714

answers:

5

I've been reviewing the features of the RichTextBox control in Silverlight 4.

What I've yet to find is any examples of Loading and Saving content in the RichTextBox.

Anyone come across any or can shed some light on it?

The control has a BlocksCollection in which I guess one could use the XamlReader to load a bunch of markup assuming that markup has a single top level node of type Block. Then add that block to the Blocks collection. Seems a shame that the RichTextBox bothers to have a "collection" in this case, why not simply a top-level Block item?

Never-the-less that still leaves saving the content of a RichTextBox, I have no idea where to begin with that one?

I'm sure I must be missing the obvious here but unless loading and saving data in to and from RichTextBox is at least possible if not easy I can't see how we can actually put it to use.

Edit

Thanks to DaveB's answer I found discussion of something called the DocumentPersister. However no reference to this class can be found in the MSDN documentation nor can I find it in the installed dlls via object browser search. Anyone, anyone at all?

+3  A: 

Check out this tutorial on the RichTextArea control. Persisting content is described in exercise 2, task 3. The code for the tutorial includes a helper class.

Edit: The question was raised about the DocumentPersister class referenced in the tutorial. It is found in the source code download for the tutorial. I think the author created it. By looking at the code you will get an idea as to persisting your data. The only downside was that if your data contained images, the helper class did not support them. Here is the link to the download.

http://ecn.channel9.msdn.com/o9/learn/Silverlight4/Labs/TextEditor/Source.zip

DaveB
Thanks Dave thats a step in the right direction, now if only I could actually find this DocumentPersister of which this article speaks.
AnthonyWJones
Thanks, so the answer is Silverlight 4 in its current Beta form provides no means to load or save "RichTextArea content", one has to resort writing it oneself or downloading some source from someone else. I do hope they sort that one out by RC time.
AnthonyWJones
There is a mistake in that example (unless it has been recently corrected) where the TextDecorations are not saved - which in silverlight is just Underline.I have a tutorial at http://cespage.com/silverlight/sl4tut14.html which contains a correction to this (in VB) for saving the TextDecorations setting, which it should be possible to infer the correction for the C# version mentioned here.
RoguePlanetoid
+1  A: 

Be wary of investing too much in the Silverlight 4 RichTextArea until it's confirmed that it will support full RichEdit functionality like bullet points/lists etc which it currently does in SL Beta 1... although I'm sure it will in RTM?

Mike Stokes
This is a good point that the RichTextArea is still in Beta - I had an issue that the Silverlight product team said is resolved in the RM release. It will at least support what it has at the moment and will hopefully add more - but this will be unknown. MIX2010 is in March where any details about the full release will be at least mentioned - if not confirmed, including RichTextArea features, examples etc.
RoguePlanetoid
A: 

I have a sample from Microsoft to persist the contents which I have to find which I will do tomorrow. I got this sample in october when beta 4 was not even announces due to which there were no tutorials available. Since that project I have not worked on silverlight so I don't know how many tutorials are available now.

ok I have found it. Where should I upload it?

Haris
+1  A: 

Just to update the link in the accepted answer, it's moved to here: http://channel9.msdn.com/learn/courses/Silverlight4/NewFeatures/RichTextBox/Introduction/

pho79
A: 

One option for loading text into RichTextBox is to use XamlReader. Dependent on the text which you are planning to load, you might need to add tag around it

public class TextToXamlConverter
{
    private const String ParaHead = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"&gt;";
    private const String ParaTail = "</Paragraph>";

    static public Paragraph Convert(string text)
    {
        String formattedText = ParaHead + text + ParaTail;
        Paragraph p = (Paragraph)XamlReader.Load(formattedText);
        return p;
    }
}
AlexEzh