Click here to Skip to main content
15,881,882 members
Articles / General Programming / String
Tip/Trick

Is Your string usable in the SL4 RichTextBox

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Mar 2011CPOL 15.5K   5
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:

C#
//-------------------------------------------------------------------------
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:

VB
'--------------------------------------------------------------------------
<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):

C#
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:

C#
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:

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):

C#
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)


Written By
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.

Comments and Discussions

 
GeneralRe: I stated it that way because I figured the property would be... Pin
#realJSOP9-Mar-11 5:14
mve#realJSOP9-Mar-11 5:14 
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
mveRichard 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
#realJSOP8-Mar-11 2:56
mve#realJSOP8-Mar-11 2:56 
No, I didn't try that. To get the proper xaml, I put a plain string in the control, and then got the contents of the Xaml property. The result is seen in the MakeAsRichTextXaml extension method above. I wannted to make double-damn sure that it was valid xaml for the Xaml property, because I use the final usage example. The nature of our project is such that the text is either going to be a plain string, or it's going to be RichTextBox-compatible xaml that we got out of the control. Everyon'e sproject is different, and this is just a general technique.

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.