Click here to Skip to main content
13,194,103 members (50,922 online)
Click here to Skip to main content
Add your own
alternative version


Posted 7 Mar 2011

Is Your string usable in the SL4 RichTextBox

Rate this:
Please Sign up or sign in to vote.
Extension method that checks for a properly Xaml-ized string
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;
    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\" 
                                <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"" 
                                <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();


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) Paddedwall Software
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.

You may also be interested in...


Comments and Discussions

GeneralRe: I stated it that way because I figured the property would be... Pin
John Simmons / outlaw programmer9-Mar-11 5:14
mvpJohn 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... Pin
AspDotNetDev8-Mar-11 11:52
protectorAspDotNetDev8-Mar-11 11:52 
GeneralDon't you need to escape the input string in your MakeAsRich... Pin
Richard Deeming14-Mar-11 10:22
memberRichard Deeming14-Mar-11 10:22 
GeneralHave you tried creating a Run of text, as in this example: h... Pin
AspDotNetDev7-Mar-11 11:42
protectorAspDotNetDev7-Mar-11 11:42 
GeneralRe: No, I didn't try that. To get the proper xaml, I put a plain... Pin
John Simmons / outlaw programmer8-Mar-11 2:56
mvpJohn Simmons / outlaw programmer8-Mar-11 2:56 

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

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

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