Category / Section
How to implement RtfText property for WinRT RichTextBoxAdv control?
You can bind Rtf text with SfRichTextBoxAdv content by implementing an extension class with RtfText 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 rtf text.
/// </summary>
public string RtfText
{
get
{
return (string)GetValue(RtfTextProperty);
}
set
{
SetValue(RtfTextProperty, 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 RtfText dependency property to enable styling, animation etc.
/// </summary>
public static readonly DependencyProperty RtfTextProperty = DependencyProperty.Register("RtfText", typeof(string), typeof(SfRichTextBoxAdvExtension), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnRtfTextChanged)));
#endregion
#region Static Events
/// <summary>
/// Called when rtf text changed.
/// </summary>
/// <param name="obj"></param>
/// <param name="e"></param>
private static void OnRtfTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
SfRichTextBoxAdvExtension richTextBox = (SfRichTextBoxAdvExtension)obj;
//Update the document with the Rtf.
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 RtfText property.
skipUpdating = true;
Stream stream = new MemoryStream();
// Saves the document as Rtf Stream.
this.Save(stream, FormatType.Rtf);
stream.Position = 0;
// Reads the stream and assigned the string to RtfText property
using (StreamReader reader = new StreamReader(stream))
{
this.RtfText = reader.ReadToEnd();
}
skipUpdating = false;
}
}
#endregion
#region Implementation
/// <summary>
/// Updates the document.
/// </summary>
/// <param name="rtfText">The Rtf text.</param>
private void UpdateDocument(string rtfText)
{
// If Rtf text property is set internally means, skip updating the document.
if (!skipUpdating && !string.IsNullOrEmpty(rtfText))
{
Stream stream = new MemoryStream();
// Convert the Rtf string to byte array.
byte[] bytes = Encoding.UTF8.GetBytes(rtfText);
// Writes the byte array to stream.
stream.Write(bytes, 0, bytes.Length);
stream.Position = 0;
//Load the Rtf stream.
Load(stream, FormatType.Rtf);
}
}
/// <summary>
/// Disposes the instance.
/// </summary>
public void Dispose()
{
this.ContentChanged -= RTE_ContentChanged;
ClearValue(RtfTextProperty);
base.Dispose();
}
#endregion
}
XAML
<local:SfRichTextBoxAdvExtension x:Name="richTextBoxAdv" Grid.Row="2" RtfText="{Binding RtfText, Mode=TwoWay}" LayoutType="Continuous" EnableMiniToolBar="False" FontFamily="Arial" FontSize="12" DocumentTitle="Note 1"/>