The RichTextBox control has a Text dependency property that allows a user to data bind content to the RichTextBox.Document property.
The RichTextBox control uses text formatters to allow a user to format the content of the RichTextBox control into any format of their choice. Three text formatters are included: PlainTextFormatter, RtfFormatter, and XamlFormatter. The RtfFormatter is the default text formatter. A user can create their own custom text formatter by creating a class that inherits from ITextFormatter and implementing its members accordlingly.
When data binding to the Text property, you must use the Text Formatter that matches the format of the underlying data. If your data is in RTF, you must use RTFFormatter; if your data is in plain text, you must use the PlainTextFormatter; if your data is XML, you must use XmlFormatter.
When using the RichTextBox with buttons to change the styles of your text such as bold, italics, etc., you will notice that the Text is not updated until the control loses focus. Therefore when you leave focus on the RichTextBox and start manipulating test with buttons, those changes will not be propogated properly. This is because by default, the source is not update until the RichTextBox control loses focus. To enable this behavior you must set the UpdateSourceTrigger to PropertyChanged on your Text property binding. This will force any change to the text to be updated through data binding to the underlying data source.
Custom text formatters can be used by implementing the ITextFormatter interface (see ITextFormatter for details and examples).
Example
The following example shows how to use a text formatter other than the default RtfFormatter. The Notes property to which Text is bound contains "This is the RichTextBox\r\n".
The following example shows how to use an RTF text formatter. The Notes property to which Text is bound contains "{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\cf0 \cf0\ql{\f2 {\ltrch This is the }{\b\ltrch RichTextBox}\li0\ri0\sa0\sb0\fi0\ql\par}}}".
Target Platforms: Windows 11, Windows 10, Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2