Click here to Skip to main content
Click here to Skip to main content

Is Your string usable in the SL4 RichTextBox

By , 7 Mar 2011
Rate this:
Please Sign up or sign in to vote.
I've been working with the new Silverlight4 RichTextBox control (which cannot properly decipher rich text from external apps, like Word, Excel, et al), and have discovered that the only way to get text into control is to set the RichTextBox.Xaml property. However, if you try to set the Xaml property to a non-Xaml string (in other words, a string that is not properly Xaml-ized), you get an exception. So, I came up with this extension method that checks to see if the string resembles a properly Xaml-ized string:
 
//-------------------------------------------------------------------------
public static bool IsRichTextXaml(string str)
{
    bool result = str.StartsWith("<Section xml") && str.EndsWith("</Section>");
    return result;
}
 
For those of you unfortunate enough to have to use VB, here's the code for that:
 
'--------------------------------------------------------------------------
<System.Runtime.CompilerServices.Extension> _
Public Function IsRichTextXaml(str As String) as Boolean
    Dim result as Boolean = str.StartsWith("<Section xml") AndAlso str.EndsWith("</Section>")
    Return result
End Function
 
Once the extension method is in place, you can do this(sorry - C# example only):
 
string myString = "This is not a Xaml-ized string";
if (myString.IsRichTextXaml())
{
    this.myRichTextBox.Xaml = myString;
}
else
{
    this.myRichTextBox.Selection.Text = myString;
}
 
You could also write an alternative extension method that simply converts a string to Xaml if necessary:
 
public static string MakeAsRichTextXaml(string str)
{
    string result = str;
    if (!str.IsRichTextXaml())
    {
        result = String.Format("<Section xml:space=\"preserve\" 
                                HasTrailingParagraphBreakOnPaste=\"False\" 
                                xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
                                <Paragraph><Run Text=\"{0}\" /></Paragraph></Section>", result)
    }
    return result;
}
 
...or for VB:
 
<System.Runtime.CompilerServices.Extension> _
Public Function MakeAsRichTextXaml(ByVal str As String) As String
    Dim result As String = str
    If (Not result.IsRichTextXaml())
        result = String.Format("<Section xml:space=""preserve"" 
                                HasTrailingParagraphBreakOnPaste=""False"" 
                                xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                                <Paragraph><Run Text=""{0}"" /></Paragraph></Section>", result)
    End If
    Return result
End Function
 
Sorry about the formatting in the last two examples, but I had to do something to try to ensure you wouldn't need a horizontal scroll to see it all. Anyway, usage would look like this (again, only a C# example):
 
string myString = "This is not a Xaml-ized string";
this.myRichTextBox.Xaml = myString.MakeAsRichTextXaml();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralRe: I stated it that way because I figured the property would be... PinmvpJohn Simmons / outlaw programmer9-Mar-11 5:14 
I stated it that way because I figured the property would be named Content (which makes a lot more sense to me than Xaml).

GeneralRe: Ok, just thought I'd mention it since you said "discovered t... PinprotectorAspDotNetDev8-Mar-11 11:52 
GeneralDon't you need to escape the input string in your MakeAsRich... PinmemberRichard Deeming14-Mar-11 10:22 
GeneralHave you tried creating a Run of text, as in this example: h... PinprotectorAspDotNetDev7-Mar-11 11:42 
GeneralRe: No, I didn't try that. To get the proper xaml, I put a plain... PinmvpJohn Simmons / outlaw programmer8-Mar-11 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 7 Mar 2011
Article Copyright 2011 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid