Category / Section
How to bind xaml text in WPF RichTextBox (SfRichTextBoxAdv) control?
2 mins read
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"/>