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

RichTextBox binding issue.

1 Answer 91 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Javed
Top achievements
Rank 1
Javed asked on 14 Jan 2013, 07:15 AM
Hi,

I am having some issues with binding RichTextBox. Problem is when I first load my form having RichTextBox, it doesnt show the Document, but when I load the form again it works fine, I have tried different methods of binding the RichTextBox as mentioned in this forum but this problem stays there..I am pasting my code below if you can find I am doing something wrong here...


XAML CODE:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<UserControl x:Class="DongEnergy.GF.AID.Client.App.Controls.RichTextBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerikRibbonView="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.RibbonView"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:common="clr-namespace:DongEnergy.GF.AID.Client.App.Common"
             mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <common:ObjectDataSource x:Key="this" />
    </UserControl.Resources>

    <StackPanel>
        <telerik:RadRichTextBoxRibbonUI Grid.Row="0" BackstageClippingElement="{Binding ElementName=gridRoot}" ApplicationName="Description" ApplicationButtonImageSource="/RichTextBox;component/Images/RichTextBox/MSOffice/AppIcon-telerik.png" 
                                        DataContext="{Binding Path=Commands, ElementName=richTextBox}" Name="radRichTextBoxRibbonUI"
                                        Margin="0 0 0 0" Padding="0 0 0 0" TitleBarVisibility="Collapsed" >
            <telerik:RadRichTextBoxRibbonUI.Resources>
                <telerik:FontFamiliesProvider x:Key="FontFamiliesProvider" />
            </telerik:RadRichTextBoxRibbonUI.Resources>
            <telerik:RadRibbonTab HeaderVisibility="Collapsed" >
                <telerik:RadRibbonGroup DialogLauncherVisibility="Visible" Header="Font" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ShowFontPropertiesDialogCommand}" telerik:ScreenTip.Description="Show the font dialog box." telerik:ScreenTip.Title="Font" telerik:ScreenTip.Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/FontDialog.png">
                    <telerik:RadOrderedWrapPanel>
                        <StackPanel Orientation="Horizontal">
                            <telerik:RadRibbonComboBox CanAutocompleteSelectItems="False" CanKeyboardNavigationSelectItems="False" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedValue}" Height="21" IsReadOnly="True" ItemsSource="{Binding Source={StaticResource FontFamiliesProvider}, Path=RegisteredFonts}" OpenDropDownOnFocus="True" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeFontFamilyCommand}" telerik:ScreenTip.Description="Change the font family." telerik:ScreenTip.Title="Font" Width="132" />
                            <telerik:RadRibbonComboBox CanAutocompleteSelectItems="False" CanKeyboardNavigationSelectItems="False" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem.Tag}" Height="21" IsEditable="True" IsReadOnly="True" IsTextSearchEnabled="False" OpenDropDownOnFocus="True" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeFontSizeCommand}" telerik:ScreenTip.Description="Change the font size." telerik:ScreenTip.Title="Font Size" Width="65" Margin="-1 0 0 0">
                                <telerik:RadRibbonComboBoxItem Content="8" Tag="10.67" />
                                <telerik:RadRibbonComboBoxItem Content="9" Tag="12" />
                                <telerik:RadRibbonComboBoxItem Content="10" Tag="13.34" />
                                <telerik:RadRibbonComboBoxItem Content="11" Tag="14.67" />
                                <telerik:RadRibbonComboBoxItem Content="12" Tag="16" />
                                <telerik:RadRibbonComboBoxItem Content="14" Tag="18.67" />
                                <telerik:RadRibbonComboBoxItem Content="16" Tag="21.33" />
                                <telerik:RadRibbonComboBoxItem Content="18" Tag="24" />
                                <telerik:RadRibbonComboBoxItem Content="20" Tag="26.67" />
                                <telerik:RadRibbonComboBoxItem Content="22" Tag="29.33" />
                                <telerik:RadRibbonComboBoxItem Content="24" Tag="32" />
                                <telerik:RadRibbonComboBoxItem Content="26" Tag="34.67" />
                                <telerik:RadRibbonComboBoxItem Content="28" Tag="37.33" />
                                <telerik:RadRibbonComboBoxItem Content="36" Tag="48" />
                                <telerik:RadRibbonComboBoxItem Content="48" Tag="64" />
                                <telerik:RadRibbonComboBoxItem Content="72" Tag="96" />
                            </telerik:RadRibbonComboBox>
                        </StackPanel>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:RadRibbonButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=IncrementFontSizeCommand}" telerik:ScreenTip.Description="Increase the font size." telerik:ScreenTip.Title="Grow Font" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/font-increasesize.png" />
                            <telerik:RadRibbonButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=DecrementFontSizeCommand}" telerik:ScreenTip.Description="Decrease the font size." telerik:ScreenTip.Title="Shrink Font" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/font-decreasesize.png" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:RadRibbonButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ClearFormattingCommand}" telerik:ScreenTip.Description="Clear all the formatting from the selection, leaving only the plain text." telerik:ScreenTip.Title="Clear Formatting" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/ClearFormatting16.png" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleBoldCommand}" telerik:ScreenTip.Description="Make the selected text bold." telerik:ScreenTip.Title="Bold" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/bold.png" />
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleItalicCommand}" telerik:ScreenTip.Description="Italicize the selected text." telerik:ScreenTip.Title="Italic" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/italic.png" />
                            <telerik:RadRibbonSplitButton IsToggle="True" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleUnderlineCommand}" telerik:ScreenTip.Description="Underline the selected text." telerik:ScreenTip.Title="Underline" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/underline.png">
                                <telerik:RadRibbonSplitButton.DropDownContent>
                                    <telerik:RadGallery ItemHeight="26" ItemWidth="156" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeUnderlineDecorationCommand}" ViewportHeight="210" ViewportWidth="157">
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline1.png" Tag="Line" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline2.png" Tag="DoubleLine" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline3.png" Tag="ThickLine" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline4.png" Tag="DottedLine" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline5.png" Tag="DashedLine" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline6.png" Tag="DotDashedLine" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline7.png" Tag="DotDotDashedLine" />
                                        <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/underline8.png" Tag="Wave" />
                                    </telerik:RadGallery>
                                </telerik:RadRibbonSplitButton.DropDownContent>
                            </telerik:RadRibbonSplitButton>
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleStrikethroughCommand}" telerik:ScreenTip.Description="Draw a line through the middle of the selected text." telerik:ScreenTip.Title="Strikethrough" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/Strikethrough16.png" />
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleSubscriptCommand}" telerik:ScreenTip.Description="Create small letters below the text baseline." telerik:ScreenTip.Title="Subscript" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/subscript.png" />
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleSuperscriptCommand}" telerik:ScreenTip.Description="Create small text above the line of text." telerik:ScreenTip.Title="Superscript" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/superscript.png" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:HighlightColorPicker AutomaticColor="Transparent" BorderThickness="0" Height="Auto" Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/highlight.png" NoColorText="No color" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeFontHighlightColorCommand}" telerik:ScreenTip.Description="Make text look like it was marked with a highlighter pen." telerik:ScreenTip.Title="Text Highlight Color" SelectedColor="Yellow" />
                            <telerik:HighlightColorPicker BorderThickness="0" Height="Auto" Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/FontForeColor.png" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeFontForeColorCommand}" telerik:ScreenTip.Description="Change the text color." telerik:ScreenTip.Title="Font Color" SelectedColor="Red" />
                        </telerik:RadButtonGroup>
                    </telerik:RadOrderedWrapPanel>
                </telerik:RadRibbonGroup>
                <telerik:RadRibbonGroup DialogLauncherVisibility="Visible" Header="Paragraph" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ShowParagraphPropertiesDialogCommand}" telerik:ScreenTip.Description="Show the Paragraph dialog box." telerik:ScreenTip.Title="Paragraph" telerik:ScreenTip.Icon="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/PropertiesDialog.png">
                    <telerik:RadOrderedWrapPanel>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleBulletsCommand}" telerik:ScreenTip.Description="Start a bulleted list." telerik:ScreenTip.Title="Bullets" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/list-bullets.png" />
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleNumberedCommand}" telerik:ScreenTip.Description="Start a numbered list." telerik:ScreenTip.Title="Numbering" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/list-numbered.png" />
                            <telerik:RadRibbonDropDownButton telerik:ScreenTip.Description="Start a multilevel list. Click the arrow to choose different multilevel list styles." telerik:ScreenTip.Title="Multilevel List" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/MultilevelList.png">
                                <telerik:RadRibbonDropDownButton.DropDownContent>
                                    <StackPanel>
                                        <ScrollViewer MaxHeight="565" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                                            <StackPanel>
                                                <telerik:RadGroupHeader Content="List Library" />
                                                <telerik:RadGallery ItemHeight="85" ItemWidth="85" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeListStyleCommand}" >
                                                    <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/MultilevelListNone.png" Tag="None" />
                                                    <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/MultilevelListBulleted.png" Tag="Bulleted" />
                                                    <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/MultilevelListNumbered.png" Tag="Numbered" />
                                                    <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/MultilevelListNumberedParenthesis.png" Tag="NumberedParenthesis" />
                                                    <telerik:RadGalleryItem Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/MultilevelListNumberedHierarchical.png" Tag="NumberedHierarchical" />
                                                </telerik:RadGallery>
                                                <telerik:ListStylesGallery AssociatedRichTextBox="{Binding Path=AssociatedRichTextBox, Mode=OneWay}" />
                                            </StackPanel>
                                        </ScrollViewer>
                                        <telerik:RadRibbonButton HorizontalAlignment="Stretch" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ShowDefineNewListStyleDialogCommand}" Size="Medium" Text="Define New List Style ..." />
                                    </StackPanel>
                                </telerik:RadRibbonDropDownButton.DropDownContent>
                            </telerik:RadRibbonDropDownButton>
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:RadRibbonButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=DecrementParagraphLeftIndentCommand}" telerik:ScreenTip.Description="Decrease the indent level of the paragraph." telerik:ScreenTip.Title="Decrease Indent" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/outdent.png" />
                            <telerik:RadRibbonButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=IncrementParagraphLeftIndentCommand}" telerik:ScreenTip.Description="Increase the indent level of the paragraph." telerik:ScreenTip.Title="Increase Indent" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/indent.png" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup MaxHeight="36">
                            <telerik:RadRibbonToggleButton CommandParameter="LeftToRight" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeParagraphFlowDirectionCommand}" telerik:ScreenTip.Description="Set the direction of text to be displayed from left to right." telerik:ScreenTip.Title="Left-To-Right Text Direction" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/LeftToRightFlowDirection.png" Text="Left-to-Right Text Direction" />
                            <telerik:RadRibbonToggleButton CommandParameter="RightToLeft" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeParagraphFlowDirectionCommand}" telerik:ScreenTip.Description="Set the direction of text to be displayed from right to left." telerik:ScreenTip.Title="Right-To-Left Text Direction" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/RightToLeftFlowDirection.png" Text="Right-to-Left Text Direction" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup>
                            <telerik:RadRibbonToggleButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ToggleFormattingSymbolsCommand}" telerik:ScreenTip.Description="Show paragraph marks and other hidden formatting symbols." telerik:ScreenTip.Title="Show/Hide Formatting Symbols" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/formatting-symbols.png" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup>
                            <telerik:RadRibbonToggleButton CommandParameter="Left" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeTextAlignmentCommand}" telerik:ScreenTip.Description="Align text to the left." telerik:ScreenTip.Title="Align Text Left" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/alignleft.png" Text="Align Left" />
                            <telerik:RadRibbonToggleButton CommandParameter="Center" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeTextAlignmentCommand}" telerik:ScreenTip.Description="Center text." telerik:ScreenTip.Title="Center" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/aligncenter.png" Text="Center" />
                            <telerik:RadRibbonToggleButton CommandParameter="Right" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeTextAlignmentCommand}" telerik:ScreenTip.Description="Align text to the right." telerik:ScreenTip.Title="Align Text Right" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/alignright.png" Text="Align Right" />
                            <telerik:RadRibbonToggleButton CommandParameter="Justify" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeTextAlignmentCommand}" telerik:ScreenTip.Description="Align text to both left and right margins, adding extra space between words as necessary." telerik:ScreenTip.Title="Justify" Size="Small" SmallImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/alignjustify.png" Text="Justify" />
                        </telerik:RadButtonGroup>
                        <telerik:RadButtonGroup>
                            <telerik:HighlightColorPicker AutomaticColor="Transparent" BorderThickness="0" MaxHeight="36" Image="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/16/ParagraphBackgroundColor.png" NoColorText="No color" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=ChangeParagraphBackgroundColorCommand}" telerik:ScreenTip.Description="Color the background behind the selected text or paragraph."  telerik:ScreenTip.Title="Shading" SelectedColor="Transparent" />
                        </telerik:RadButtonGroup>
                    </telerik:RadOrderedWrapPanel>
                </telerik:RadRibbonGroup>
                <telerik:RadRibbonGroup Header="Insert">
                    <telerik:RadRibbonButton LargeImage="/Telerik.Windows.Controls.RichTextBoxUI;component/Images/MSOffice/32/Picture.png" telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding Path=InsertPictureCommand}" telerik:ScreenTip.Description="Insert a picture from a file." telerik:ScreenTip.Title="Insert Picture from File" Size="Large" Text="Picture" />
                </telerik:RadRibbonGroup>
            </telerik:RadRibbonTab>
        </telerik:RadRichTextBoxRibbonUI>

        <!--<telerik:HtmlDataProvider Html="{Binding HtmlDocument, Source={StaticResource this}, Mode=TwoWay}" RichTextBox="{Binding ElementName=richTextBox}" x:Name="dataProvider" />-->
        <telerik:RadRichTextBox Grid.Row="1" x:Name="richTextBox" IsSpellCheckingEnabled="False" LayoutMode="Flow" BorderThickness="2"  
                                        FontSize="8" Height="250" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="0 0 0 0" Padding="0 0 0 0"   >
            <telerik:RadRichTextBox.Resources>
                <telerik:HtmlDataProvider x:Key="HtmlDataProvider"  RichTextBox="{Binding ElementName=richTextBox}" Html="{Binding HtmlDocument, Source={StaticResource this}, Mode=TwoWay}"  />
            </telerik:RadRichTextBox.Resources>
            
        </telerik:RadRichTextBox>
    </StackPanel>
</UserControl>

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

CODE BEHIND
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Documents.FormatProviders;
using Telerik.Windows.Documents.FormatProviders.Html;
using Telerik.Windows.Documents.Model;

namespace DongEnergy.GF.AID.Client.App.Controls
{
    /// <summary>
    /// Rich text box control
    /// </summary>
    public partial class RichTextBox : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RichTextBox" /> class.
        /// </summary>
        public RichTextBox()
        {
            InitializeComponent();
            richTextBox.LostFocus += RichTextBoxLostFocus;
        }

        void RichTextBoxLostFocus(object sender, System.EventArgs e)
        {
            HtmlDocument = ExportHtml(((RadRichTextBox) sender).Document);
        }

        #region Dependency Properties

        /// <summary>
        /// The document property
        /// </summary>
        public static readonly DependencyProperty HtmlDocumentProperty = DependencyProperty.Register("HtmlDocument", typeof(string), 
                                                                      typeof (RichTextBox),
                                                                      new PropertyMetadata(OnHtmlDocumentPropertyChanged));
        /// <summary>
        /// Gets or sets the HTML value.
        /// </summary>
        /// <value>
        /// The HTML value.
        /// </value>
        public string HtmlDocument
        {
            get
            {
                return (string) GetValue(HtmlDocumentProperty);
            }
            set 
            { 
                SetValue(HtmlDocumentProperty, value);
            }
        }

        /// <summary>
        /// Called when [HTML document property changed].
        /// </summary>
        /// <param name="dependencyObject">The dependency object.</param>
        /// <param name="eventArgs">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
        private static void OnHtmlDocumentPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            var richEdit = (RichTextBox)dependencyObject;

            if (string.IsNullOrEmpty((string)eventArgs.NewValue) == false)
            {
                richEdit.richTextBox.Document = ImportHtml(eventArgs.NewValue.ToString());
            }
        }

        /// <summary>
        /// Imports the HTML.
        /// </summary>
        /// <param name="content">The content.</param>
        /// <returns></returns>
        private static RadDocument ImportHtml(string content)
        {
            IDocumentFormatProvider provider = new HtmlFormatProvider();
            RadDocument document;
            using (var stream = new MemoryStream())
            {
                var writer = new StreamWriter(stream);
                writer.Write(content);
                writer.Flush();
                stream.Seek(0, SeekOrigin.Begin);
                document = provider.Import(stream);
            }

            return document;
        }

        /// <summary>
        /// Exports the HTML.
        /// </summary>
        /// <param name="document">The document.</param>
        /// <returns></returns>
        private static string ExportHtml(RadDocument document)
        {
            IDocumentFormatProvider exporter = new HtmlFormatProvider();
            string result;

            using (var stream = new MemoryStream())
            {
                exporter.Export(document, stream);
                stream.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(stream))
                {
                    result = reader.ReadToEnd();
                }
            }

            return result;
        }

        #endregion
    }
}



1 Answer, 1 is accepted

Sort by
0
Boby
Telerik team
answered on 15 Jan 2013, 07:12 AM
Hi Javed,
You can read more about how to bind RadRichTextBox in the help article about data providers.

All the best,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
RichTextBox
Asked by
Javed
Top achievements
Rank 1
Answers by
Boby
Telerik team
Share this question
or