Category / Section
How to bind xaml text in WPF RichTextBox (SfRichTextBoxAdv) control?
You can bind XAML text with WPF RichTextBox (SfRichTextBoxAdv) content by extending the SfRichTextBoxAdv class with the XamlText property.
Please refer to 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>
/// Used as a backing store for the 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 the XAML text changes.
/// </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 updating of the document when setting the XamlText property.
skipUpdating = true;
Stream stream = new MemoryStream();
// Saves the document as a XAML stream.
this.Save(stream, FormatType.Xaml);
stream.Position = 0;
// Reads the stream and assigns the string to the 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 the XamlText property is set internally means, skip updating the document.
if (!skipUpdating && !string.IsNullOrEmpty(xamlText))
{
Stream stream = new MemoryStream();
// Convert the XAML string to a byte array.
byte[] bytes = Encoding.UTF8.GetBytes(xamlText);
// Writes the byte array to the stream.
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
// Load the XAML stream.
Load(stream, FormatType.Xaml);
}
}
/// <summary>
/// Disposes of 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"/>