This is a migrated thread and some comments may be shown as answers.

Can I convert 'MsRichTextBoxXaml' to 'RadRichTextBox Xaml'?

4 Answers 65 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
SPE
Top achievements
Rank 1
SPE asked on 24 Nov 2011, 09:26 AM

Hi,

I have many string datas in RichTextBoxXaml format.

Like this,

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns=" FontSize="12" FontFamily="Gulim" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run Foreground="#FFFF0000" Text="asdf" /></Paragraph><Paragraph FontSize="12" FontFamily="Gulim" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run Foreground="#FFFF0000" /></Paragraph><Paragraph FontSize="12" FontFamily="Gulim" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left"><Run Foreground="#FF000000" Text="qwer" />
</Paragraph></Section>



What I want to do is,

to convert this data into RadRichTextBox Xaml format.

Such as,

<t:RadDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents" version="1.1" DefaultPageLayoutSettings="816,1056" LayoutMode="Flow" PageViewMargin="10,10">    <t:Section>      <t:Paragraph>        <t:Span FontFamily="Verdana" FontSize="16" ForeColor="#FFFF0000" Text="asdfasdfwqtr..!!!!! dafsfd dafdsafsdfasdfasf" UnderlineColor="#FF000000" UnderlineDecoration="None" />      </t:Paragraph>    </t:Section>  </t:RadDocument>



I tried below code.

        private string ConvertMsXamlToTelerikXaml(string msXaml)
        {
            MsRichTextBoxXamlFormatProvider xamlProvider = new MsRichTextBoxXamlFormatProvider();
            var document = xamlProvider.Import(msXaml);
            RadRichTextBox richBox = new RadRichTextBox()
            {
                Document = document
            };

            XamlDataProvider provider = new XamlDataProvider();
            provider.RichTextBox = richBox;

            return provider.Xaml;
        }




But it always gives me a paragraph without any value.

- <t:RadDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents" version="1.1" DefaultPageLayoutSettings="816,1056" LayoutMode="Flow" PageViewMargin="10,10">
- <t:Section>
<t:Paragraph />
</t:Section>
</t:RadDocument>


How can I make telerik xaml successfully?

Thank you in advance.

4 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 29 Nov 2011, 11:40 AM
Hi Sunmin,

The problem is that Section elements are not supported on import. As described in MSDN Sections are not part of the document model of RichTextBox.

If you modify your XAML to the following format:

<Paragraph FontSize="12" FontFamily="Gulim" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
    <Run Foreground="#FFFF0000" Text="asdf" />
</Paragraph>
<Paragraph FontSize="12" FontFamily="Gulim" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
    <Run Foreground="#FFFF0000" />
</Paragraph>
<Paragraph FontSize="12" FontFamily="Gulim" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" TextAlignment="Left">
    <Run Foreground="#FF000000" Text="qwer" />
</Paragraph>

 it could be imported with MsXamlFormatProvider and exported with XamlFormatProvider in a way similar to the one in your snippet.

I hope this helps.

Regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
SPE
Top achievements
Rank 1
answered on 30 Nov 2011, 01:38 AM

Hello,

Thank you for your help.

But what you recommended doesn't seem to work.



My snippet is below.


        public MainPage()
        {
            InitializeComponent();

            string textToConvert = @"<ParagraphFontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000""
FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left"">
    <RunForeground=""#FFFF0000"" Text=""asdf""/>
</Paragraph>
<ParagraphFontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal""
FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left"">
    <RunForeground=""#FFFF0000""/>
</Paragraph>
<ParagraphFontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal""
FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left"">
    <RunForeground=""#FF000000"" Text=""qwer""/>
</Paragraph>";

            string result = ConvertMsXamlToTelerikXaml(textToConvert);
        }

        private string ConvertMsXamlToTelerikXaml(string msXaml)
        {
            MsRichTextBoxXamlFormatProvider xamlProvider = new MsRichTextBoxXamlFormatProvider();
            var document = xamlProvider.Import(msXaml);
            RadRichTextBox richBox = new RadRichTextBox()
            {
                Document = document
            };
            XamlDataProvider provider = new XamlDataProvider();
            provider.RichTextBox = richBox;
            return provider.Xaml;
        }





In runtime, it throws an exception.


I really appreciate your help.

Thank you.
0
Accepted
Iva Toteva
Telerik team
answered on 02 Dec 2011, 02:39 PM
Hi Sunmin,

Actually I was mistaken in my previous reply.
The XAML serialization of the rich text boxes indeed looks like the snippets in your first post. The problem is that there is an interval just after the opening quotation mark in the declaration of xmlns. The whole string should look like this (after being escaped in VisualStudio):

string textToConvert = @"<Section xml:space=""preserve"" HasTrailingParagraphBreakOnPaste=""False"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Paragraph FontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left""><Run Foreground=""#FFFF0000"" Text=""asdf"" /></Paragraph><Paragraph FontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left""><Run Foreground=""#FFFF0000"" /></Paragraph><Paragraph FontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left""><Run Foreground=""#FF000000"" Text=""qwer"" />
</Paragraph></Section>";

On a side note, DataProviders have been implemented to help you bind the content of the document. If you just want to import and export the document, you should use XamlFormatProvider.
In a nutshell, the code that can be used to convert  the string above to our XAML format is:
            string textToConvert = @"<Section xml:space=""preserve"" HasTrailingParagraphBreakOnPaste=""False"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
<Paragraph FontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left""><Run Foreground=""#FFFF0000"" Text=""asdf"" /></Paragraph><Paragraph FontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left""><Run Foreground=""#FFFF0000"" /></Paragraph><Paragraph FontSize=""12"" FontFamily=""Gulim"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left""><Run Foreground=""#FF000000"" Text=""qwer"" />
</Paragraph></Section>";
 
            MsRichTextBoxXamlFormatProvider provider = new MsRichTextBoxXamlFormatProvider();
            RadDocument document = provider.Import(textToConvert);
            XamlFormatProvider telerikXamlFP = new XamlFormatProvider();
            string telerikXaml = telerikXamlFP.Export(document);

You can also find attached a demo illustrating the export and import. Kind regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
SPE
Top achievements
Rank 1
answered on 05 Dec 2011, 01:27 AM

Hi, Iva Toteva!

Your recommendation is great and I resolved my problem with your help.

Thank you very much.

Have a nice day~~~  : )

Tags
RichTextBox
Asked by
SPE
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
SPE
Top achievements
Rank 1
Share this question
or