Domanda

I have a Silverlight control 5 that is hosted on a WebPage.

Im trying to load in the RTF text into the Silverlight RichTextBlock but I cant find anyway of donig this.

The info on MSDN refers to adding new content to the control but not loading/parsing an actual RTF String.

In C# Id like to do this;

myRTB.Rtf = myrtfString;

But there isnt a Rtf property!

È stato utile?

Soluzione

The RichTextBox, despite its misleading name, does not support RTF. You have to convert your RTF source to XAML.I use a way to do this,

use a FlowDocument to change the format from rtf to xaml. Then remove the attributes not accepted in SL4 richtext box, codes like below.

string xaml = String.Empty;
FlowDocument doc = new FlowDocument();
TextRange range = new TextRange(doc.ContentStart, doc.ContentEnd);

using (MemoryStream ms = new MemoryStream())
{
    using(StreamWriter sw = new StreamWriter(ms))
    {
        sw.Write(from);
        sw.Flush();
        ms.Seek(0, SeekOrigin.Begin);
        range.Load(ms, DataFormats.Rtf);
    }
}


using(MemoryStream ms = new MemoryStream())
{
    range = new TextRange(doc.ContentStart, doc.ContentEnd);

    range.Save(ms, DataFormats.Xaml);
    ms.Seek(0, SeekOrigin.Begin);
    using (StreamReader sr = new StreamReader(ms))
    {
        xaml = sr.ReadToEnd();
    }
}

// remove all attribuites in section and remove attribute margin 

int start = xaml.IndexOf("<Section");
int stop = xaml.IndexOf(">") + 1;

string section = xaml.Substring(start, stop);

xaml = xaml.Replace(section, "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
xaml = xaml.Replace("Margin=\"0,0,0,0\"", String.Empty);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top