WPF RichTextBox With Two Way DataBinding

by Shawn Duggan on 02/08/2009

In an application I’m currently writing my users will be creating many text notes and I wanted to allow for the user to create nicely formatted documents, as well as having the ability for fine-grained control over printing options.  Enter the RichTextBox – it is similar to a TextBox, but provides more advanced text formatting options to the user.  Enabling the advanced editing features is trivial (read up on the available Editing Commands), literally in minutes you can create a WYSIWYG editor.   Where things get sticky is when you start talking about databinding.

The RichTextBox stores the text in its Document property which is of type FlowDocument.  It isn’t a dependency property, nor does it implement INotiftyPropertyChanged so you can’t bind directly to it.  This is a problem.  In my application I want my users to create richly formatted documents which are added to a Notes collection on my entity and then have each note persisted as a string in my database.

I decided the best way to go would be to subclass the RichTextBox, and expose a DependencyProperty to handle the binding.  I’d then need some plumbing internally to keep things in sync.  Before I plunged headfirst into my own implementation, I spent a little time on Google to see how others have approached the problem.  I found many people hoping to do the same thing I was, but not any complete solutions.  I did find a MSDN forum post which had a code snippet that looked like a good place to start, and I also found some sample code for a WYSIWYG editor, so I cobbled the 2 together in my new WPFControls project and I was off and running.

Getting the Two-Way databinding working did turn out to be more of a chore than I’d hoped, but using the IValueConverter interface I was eventually able to bind a string property on a domain entity to the Document property on a RichTextBox.  I decided to wrap the implementation up in its own UserControl for reuse, so in the end my domain entity is bound to a string DP on the UC, and the Document property on the RichTextBox is bound to this same string property on the UC using IValueConverter.

To use the control in a project you first need to reference it in your project, and then in xaml you can reference it like this:

   1: xmlns:WPFControls="clr-namespace:BlueTango.WPFControls;assembly=BlueTango.WPFControls"

and then to use it:

   1: <WPFControls:TextEditor
   2:                     SpellCheckIsEnabled="True"
   3:                     AcceptsTab="True"
   4:                     Document="{Binding ElementName=lstNotes, Path=SelectedItem.Description, Mode=TwoWay}" />

I added a couple of properties on the control itself to illustrate that it easy to still have control over the inner wrapped RTB.  You can see in my sample above I have it bound to a ListBox control which has a list of Notes.  The Description property is the string holding the xaml of the FlowDocument.

I have included a rough sample project you can use to create your own control for your projects.  Enjoy!

BlueTango.WPFControls.zip (2777)

UPDATE:   A reader contributed this code a while back…

From Abbott Fleur and Dan Vanderboom: “I’m expanding your BindableRichTextBox control to allow linq-sql binding to db text source. I’m planning on using an existing routine I’ve written that loads the RTB based on the ip format and saves based on a SaveAs (DataFormats) property. ”

While I have not tried this code myself – here it is.  Thanks Abbott and Dan!

BoundRichTextBox.zip (2090)

While comments are welcome, please note that I am unable to offer help or support for using this update or the original code.  It is provided “as-is” without warranty or guarantees.

{ 64 comments… read them below or add one }

51 Juan August 4, 2014 at 3:01 am

I do not even know how I ended up here, but
I thought this post was good. I do not know who you
are but certainly you are going to a famous blogger if you are
not already ;) Cheers!

52 Jonah August 4, 2014 at 6:24 am

A fascinating discussion is worth comment. I believe
that you should write more on this topic, it may not be a taboo subject but generally people don’t
speak about such topics. To the next! Cheers!!

53 Carmen August 4, 2014 at 3:36 pm

If some one needs expert view about blogging and site-building afterward i advise him/her to pay a quick visit this website,
Keep up the good job.

54 Petra August 6, 2014 at 7:05 am

This page certainly has all the information I needed concerning this subject and didn’t know who
to ask.

55 sante canada August 13, 2014 at 10:53 pm

Vous avez dit ceci vraiment bien!

56 Glaucoma Surgery August 16, 2014 at 8:52 pm

Hi there this is kind of of off topic but
I was wondering if blogs use WYSIWYG editors or if you have to
manually code with HTML. I’m starting a blog
soon but have no coding skills so I wanted to get guidance from someone with
experience. Any help would be greatly appreciated!

57 how to file Bankruptcy August 26, 2014 at 2:21 am

Hey There. I found your weblog the usage of msn. That is a very smartly written article.
I’ll make sure to bookmark it and come back to learn more of your useful information. Thank you for the post.
I will definitely return.

58 Mitzi August 26, 2014 at 9:18 pm

It might deploy Installer.app that’ll put in a lot of third party iPod Software
to the unit. You are able to eliminate the application by going to ‘Packages’.
The iPad 2 had an excellent screen of just one,024 x
768.

59 fused quartz tube August 28, 2014 at 6:05 am

In a third embodiment, the absolute refractive index
of the quartz article is less than or equal to 1.
This reaction leads to very bright light, which is then used
in the projector. U-boats – World War I saw submarines making a significant impact.

60 install whatsapp on pc free August 29, 2014 at 7:37 am

Virus/Spyware is a program that been developed to damage your computer and
sometimes your privacy too. The next photograph storage
option that I would recommend is Flickr. Tear stated that BB doesn’t
see that as a concern.

61 Tegan August 31, 2014 at 8:13 am

Keepp on working, great job!

62 download ireb for windows error 1015 September 4, 2014 at 2:38 pm

Tugboats were intended to tow large ships. These will be mount’s most common kind.
A proven way is to get writing classes. Learn to create and proceed to enhance your capabilities.

63 Tamela September 5, 2014 at 10:53 am

It actually allows running illegitimate applications.

64 Chelsea September 9, 2014 at 9:00 pm

Sure…hops, in a Russian Imperial Stout nonetheless. Check out their
rating with the Better Business Bureau and also see how
long they have been around. It will only enhance it additional and make it open to other networks.

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: