Getting the Data Out
Now that you've got a WPF RichTextBox on your page and supporting some nifty edit commands, you need to find a way to get to the document content that users have entered into the control. Seasoned programmers probably got lockjaw from the moment they realized this was a big enough topic to warrant an entire section. Unfortunately, it does
warrant an entire section.
The RichTextBox works with XAML natively to build out all of the text effects you see and enable the capabilities that were absent from previous versions. When you read the value (contents), what you'll get is XAML code. A large contingent of the application development community probably has all of their rich formatted data stored in HTML format for portability purposes and will have to write their own HTML ( XAML ( HTML conversion routines. Although not complete, a Microsoft Channel 9 blogger created a reference implementation routine for doing just this that you can download
. It's not perfect but it saves you from having to build the routine yourself for simple HTML/XAML conversions.
To get the actual XAML created by the user inside of the RichTextBox, you create a TextRange, and then do some stream processing to read the XAML. The example below uses a memory stream to read the value from the control, and then uses the ASCIIEncoding object to translate it to a string for storage in a database, file, or other data store:
TextRange tr = new TextRange(
MemoryStream ms = new MemoryStream();
string xamlString =
The preceding code retrieves the entire contents of the RichTextBox by creating a TextRange that starts at the beginning of the document and extends all the way to the end. Although this is more complicated than just getting the value from a Text
property it enables you to create much more complex applications using the RichTextBox as a base. You can create smaller text ranges that cover specific portions of a rich text document to develop different functionality.
Modifying the Interface
|Figure 3. A Date Button: The Date button added to the toolbar inserts the current date into a TextRange.|
One common scenario would be to add a button to your control that might get some data from a database and dynamically insert it into the RichTextBox document. This simplified example just inserts the current date.
You use the TextRange object to select what part of the RichTextBox's document to update. In Figure 3
, a toolbar button lets users insert the current date.
In the Click event handler for the button, create a text range based on the current selection, using the Selection.Start
properties. Setting the Text
property of this TextRange to the current date string replaces whatever text is currently selected. If you just wanted the date to be inserted at the current cursor position without replacing anything, you would create the TextRange using the Selection.Start
value for both the starting and ending point of your TextRange.
protected void InsertCurrentDate
(object sender, EventArgs e)
TextRange tr = new TextRange(
tr.Text = DateTime.Now.ToShortDateString();
Creative use of TextRange based updating allows multiple users to change the same RichTextBox in real-time though the internet as well as any number of other creative possibilities, such as inline word suggestion similar to IntelliSense.