There has been a new control added to the Extended WPF Toolkit project on CodePlex called the RichTextBox.

The RichTextBox extends the System.Windows.Control.RichTextBox control that represents a rich editing control which operates on FlowDocument objects. The RichTextBox control has a Text dependency property which allows a user to data bind content to the RichTextBox.Document property. The RichTextBox control introduces the concept of Text Formatters. Text Formatters allows a user to format the content of the RichTextBox control into any format of their choice. Three Text Formatters are included; PlainTextFormatter, RtfFormatter, and a 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 implimenting the contract accordlingly.

Usage

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 the RTF formatter. If your data is in plain text, you must use the PlainTextFormatter.

richtextbox_control.jpg

This RichTextBox is bound to an object that has a Notes property. The value of the Notes property is as follows in the RTF format:

{rtf1ansiansicpg1252uc1htmautspdeff2{fonttbl{f0fcharset0 Times New Roman;}{f2fcharset0 Segoe UI;}}{colortblred0green0blue0;red255green255blue255;}lochhichdbchpardplainltrparitap0{lang1033fs18f2cf0 cf0ql{f2 {ltrch This is the }{bltrch RichTextBox}li0ri0sa0sb0fi0qlpar}}}

<toolkit:RichTextBox x:Name="_richTextBox" Grid.Row="1" Margin="10" BorderBrush="Gray" Padding="10"
                             Text="{Binding Notes}"
                             ScrollViewer.VerticalScrollBarVisibility="Auto" />

 

Using Formatters

To use a different Text Formatter than the default RtfFormatter use the following syntax:

PlainTextFormatter

<toolkit:RichTextBox x:Name="_richTextBox" Grid.Row="1" Margin="10" BorderBrush="Gray" Padding="10"
                                     Text="{Binding Notes}"
                                     ScrollViewer.VerticalScrollBarVisibility="Auto">
            <toolkit:RichTextBox.TextFormatter>
                <toolkit:PlainTextFormatter />
            </toolkit:RichTextBox.TextFormatter>
        </toolkit:RichTextBox>

XamlFormatter

<toolkit:RichTextBox x:Name="_richTextBox" Grid.Row="1" Margin="10" BorderBrush="Gray" Padding="10"
                                     Text="{Binding Notes}"
                                     ScrollViewer.VerticalScrollBarVisibility="Auto">
            <toolkit:RichTextBox.TextFormatter>
                <toolkit:XamlFormatter />
            </toolkit:RichTextBox.TextFormatter>
        </toolkit:RichTextBox>

 

Custom Formatters

To create a custom formatter, create a class that inherits from ITextFormatter and implement accordingly.

public class MyFormatter : ITextFormatter
    {
        public string GetText(System.Windows.Documents.FlowDocument document)
        {
            return new TextRange(document.ContentStart, document.ContentEnd).Text;
        }

        public void SetText(System.Windows.Documents.FlowDocument document, string text)
        {
            new TextRange(document.ContentStart, document.ContentEnd).Text = text;
        }
    }

XAML

<toolkit:RichTextBox x:Name="_richTextBox" Grid.Row="1" Margin="10" BorderBrush="Gray" Padding="10"
                            Text="{Binding Notes}"
                            ScrollViewer.VerticalScrollBarVisibility="Auto">
            <toolkit:RichTextBox.TextFormatter>
                <myCustomFormatter:MyFormatter />
            </toolkit:RichTextBox.TextFormatter>
        </toolkit:RichTextBox>

Brian Lagunas

View all posts

Follow Me

Follow me on Twitter, subscribe to my YouTube channel, and watch me stream live on Twitch.