WPF Binding to RichTextBox.Document

I was getting some nice crashes on startup while trying to do some data binding to the Document property on RichTextBox under WPF and so I hit the net and found out that it's not possible. I quickly came up with the following code, which seems to work:

class BindableRichTextBox : RichTextBox
{
    public static readonly DependencyProperty DocumentProperty =
DependencyProperty.Register("Document", typeof(FlowDocument), typeof(BindableRichTextBox), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(OnDocumentChanged)));

    public new FlowDocument Document
    {
        get { return (FlowDocument)GetValue(DocumentProperty); }
        set { SetValue(DocumentProperty, value); }
    }

    static void OnDocumentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
    {
        RichTextBox rtb = (RichTextBox)obj;
        rtb.Document = (FlowDocument)args.NewValue;
    }

}

Now the problem I have is that I don't know why the framework doesn't do this itself and if it's going to cause me massive problems further down the road.

posted @ Thursday, October 04, 2007 11:10 AM

Print

Comments on this entry:

# re: WPF Binding to RichTextBox.Document

Left by Sandeep at 10/4/2007 9:42 PM
Gravatar
Hi Jon,
I too am facing the same problem. I went through your code posted above. However, what piece of code do you write in the xaml to bind to the source along with this piece of code in the class file to get the data into RichTextBox.
Your help will be highly appreciated.
Thanks,
Sandeep

# re: WPF Binding to RichTextBox.Document

Left by Jon at 10/5/2007 5:18 PM
Gravatar
My binding is this:
Document="{Binding Path=YourDocProperty, ElementName=richTextEditor, Mode=OneWay}"

richTextEditor is just the name of the user control I've wrapped it all up in, you can of course use one of the other ways to map to a property

# re: WPF Binding to RichTextBox.Document

Left by Sandeep at 10/8/2007 4:12 PM
Gravatar
Jon,
I am still not able to make it work. Did u create a separate user control for this? And are you using <local:BindableRichTextBox> as the node in xaml ( assuming local represents the class for the namespace of the project containing BindableRichTextBox) to represnt the RichTextBox node?
It will be great if you please share the piece of code relevant to the context. I have to make this work urgently.
TIA,
Sandeep

# re: WPF Binding to RichTextBox.Document

Left by Jon at 10/8/2007 11:40 PM
Gravatar
All it should take (simple case) is a property (must be a property, member variable will not do) of type FlowDocument (name being some replacement for YourDocProperty) in the class that contains the <namespacename:BindableRichTextBox> item in it's XAML. What I have is a custom control that combines my new class and a toolbar (x:Name="richTextEditor") and so the binding is a straight link to that.

# re: WPF Binding to RichTextBox.Document

Left by Chris at 11/11/2007 4:27 AM
Gravatar
I have tried this way before, but dropped it since I couldn't get the page GCed that had a binding to the Document property. I couldn't figure out where in the bindings it was holding a reference, so gave up on it :) Have you had any of these problems?
Comments have been closed on this topic.