ExtendedRichTextEditor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.IO;
  2. using System.Text;
  3. using System.Windows;
  4. using Syncfusion.Windows.Controls.RichTextBoxAdv;
  5. namespace InABox.DynamicGrid
  6. {
  7. /// <summary>
  8. /// Represents the extension class for SfRichTextBoxAdv.
  9. /// </summary>
  10. public class ExtendedRichTextEditor : SfRichTextBoxAdv
  11. {
  12. #region Static Dependency Properties
  13. /// <summary>
  14. /// Using as a backing store for Text dependency property to enable styling, animation etc.
  15. /// </summary>
  16. public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(ExtendedRichTextEditor),
  17. new PropertyMetadata(string.Empty, OnTextChanged));
  18. #endregion
  19. #region Fields
  20. private bool skipUpdating;
  21. #endregion
  22. #region Constructor
  23. /// <summary>
  24. /// Initializes the instance of SfRichTextBoxAdvExtension class.
  25. /// </summary>
  26. public ExtendedRichTextEditor()
  27. {
  28. // Wires the ContentChanged event.
  29. ContentChanged += RTE_ContentChanged;
  30. }
  31. #endregion
  32. #region Properties
  33. /// <summary>
  34. /// Gets or Sets the text.
  35. /// </summary>
  36. public string Text
  37. {
  38. get => (string)GetValue(TextProperty);
  39. set => SetValue(TextProperty, value);
  40. }
  41. #endregion
  42. #region Static Events
  43. /// <summary>
  44. /// Called when text changed.
  45. /// </summary>
  46. /// <param name="obj"></param>
  47. /// <param name="e"></param>
  48. private static void OnTextChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
  49. {
  50. var richTextBox = (ExtendedRichTextEditor)obj;
  51. //Update the document with the text.
  52. richTextBox.UpdateDocument((string)e.NewValue);
  53. }
  54. #endregion
  55. #region Events
  56. /// <summary>
  57. /// Called when content changes in SfRichTextBoxAdv.
  58. /// </summary>
  59. /// <param name="obj"></param>
  60. /// <param name="args"></param>
  61. private void RTE_ContentChanged(object obj, ContentChangedEventArgs args)
  62. {
  63. if (Document != null)
  64. {
  65. // To skip internal updation of document on setting Text property.
  66. skipUpdating = true;
  67. Stream stream = new MemoryStream();
  68. // Saves the document as text Stream.
  69. Save(stream, FormatType.Html);
  70. stream.Position = 0;
  71. // Reads the stream and assigned the string to Text property
  72. using (var reader = new StreamReader(stream))
  73. {
  74. Text = reader.ReadToEnd();
  75. }
  76. skipUpdating = false;
  77. }
  78. }
  79. #endregion
  80. #region Implementation
  81. /// <summary>
  82. /// Updates the document.
  83. /// </summary>
  84. /// <param name="text">The text.</param>
  85. private void UpdateDocument(string text)
  86. {
  87. // If text property is set internally means, skip updating the document.
  88. if (!skipUpdating && !string.IsNullOrEmpty(text))
  89. {
  90. Stream stream = new MemoryStream();
  91. // Convert the text string to byte array.
  92. var bytes = Encoding.UTF8.GetBytes(text);
  93. // Writes the byte array to stream.
  94. stream.Write(bytes, 0, bytes.Length);
  95. stream.Position = 0;
  96. //Load the Text stream.
  97. Load(stream, FormatType.Html);
  98. }
  99. }
  100. /// <summary>
  101. /// Disposes the instance.
  102. /// </summary>
  103. public void Dispose()
  104. {
  105. ContentChanged -= RTE_ContentChanged;
  106. ClearValue(TextProperty);
  107. base.Dispose();
  108. }
  109. #endregion
  110. }
  111. }