We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind xaml text in WPF RichTextBox (SfRichTextBoxAdv) control?

Platform: WPF |
Control: SfRichTextBoxAdv |
Published Date: September 9, 2015 |
Last Revised Date: July 22, 2021 |
.NET Framework: 3.5

You can bind Xaml text with WPF RichTextBox (SfRichTextBoxAdv) content by extending SfRichTextBoxAdv class with XamlText property.

Please refer the following code samples.
C#

/// <summary>
/// Represents the extension class for SfRichTextBoxAdv.
/// </summary>
public class SfRichTextBoxAdvExtension : SfRichTextBoxAdv
{
    #region Fields
    bool skipUpdating = false;
    #endregion
 
    #region Properties
    /// <summary>
    /// Gets or Sets the xaml text.
    /// </summary>
    public string XamlText
    {
        get
        {
            return (string)GetValue(XamlTextProperty);
        }
        set
        {
            SetValue(XamlTextProperty, value);
        }
    }
    #endregion
 
    #region Constructor
    /// <summary>
    /// Initializes the instance of SfRichTextBoxAdvExtension class.
    /// </summary>
    public SfRichTextBoxAdvExtension()
    {
        // Wires the ContentChanged event.
        this.ContentChanged += RTE_ContentChanged;
    }
    #endregion
 
    #region Static Dependency Properties
    /// <summary>
    /// Using as a backing store for XamlText dependency property to enable styling, animation etc.
    /// </summary>
    public static readonly DependencyProperty XamlTextProperty = DependencyProperty.Register("XamlText", typeof(string), typeof(SfRichTextBoxAdvExtension), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnXamlTextChanged)));
    #endregion
 
    #region Static Events
    /// <summary>
    /// Called when xaml text changed.
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="e"></param>
    private static void OnXamlTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        SfRichTextBoxAdvExtension richTextBox = (SfRichTextBoxAdvExtension)obj;
        //Update the document with the XAML.
        richTextBox.UpdateDocument((string)e.NewValue);
    }
    #endregion
 
    #region Events
    /// <summary>
    /// Called when content changes in SfRichTextBoxAdv.
    /// </summary>
    /// <param name="obj"></param>
    /// <param name="args"></param>
    void RTE_ContentChanged(object obj, ContentChangedEventArgs args)
    {
        if (this.Document != null)
        {
            // To skip internal updation of document on setting XamlText property.
            skipUpdating = true;
            Stream stream = new MemoryStream();
            // Saves the document as Xaml Stream.
            this.Save(stream, FormatType.Xaml);
            stream.Position = 0;
            // Reads the stream and assigned the string to XamlText property
            using (StreamReader reader = new StreamReader(stream))
            {
                this.XamlText = reader.ReadToEnd();
            }
            skipUpdating = false;
        }
    }
    #endregion
 
    #region Implementation
    /// <summary>
    /// Updates the document.
    /// </summary>
    /// <param name="xamlText"></param>
    private void UpdateDocument(string xamlText)
    {
        // If Xaml text property is set internally means, skip updating the document.
        if (!skipUpdating && !string.IsNullOrEmpty(xamlText))
        {
            Stream stream = new MemoryStream();
            // Convert the Xaml string to byte array.
            byte[] bytes = Encoding.UTF8.GetBytes(xamlText);
            // Writes the byte array to stream.
            stream.Write(bytes, 0, bytes.Length);
            stream.Position = 0;
            //Load the Xaml stream.
            Load(stream, FormatType.Xaml);
        }
    }
    /// <summary>
    /// Disposes the instance.
    /// </summary>
    public void Dispose()
    {
        this.ContentChanged -= RTE_ContentChanged;
        ClearValue(XamlTextProperty);
        base.Dispose();
    }
    #endregion
}

XAML

<local:SfRichTextBoxAdvExtension x:Name="richTextBoxAdv" Grid.Row="2" XamlText="{Binding XamlText, Mode=TwoWay}" LayoutType="Continuous" EnableMiniToolBar="False" FontFamily="Arial" FontSize="12" DocumentTitle="Note 1"/>

View sample in GitHub.

2X faster development

The ultimate WPF UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile