0

My guess is I'm not understanding attached properties correctly. I'm trying to convert the FlowDocument on a RichTextBox to an HTML string property in my view model. I have two RichTextBoxes that are using my RichTextBoxAssistant class (thanks to this blog post):

<RichTextBox x:Name="rtb_description" 
    local:RichTextBoxAssistant.BoundDocument="{Binding MyVM.Description,
        ValidatesOnDataErrors=True}"/>

<RichTextBox x:Name="rtb_descriptionHowTo"
    local:RichTextBoxAssistant.BoundDocument="{Binding MyVM.DescriptionHowTo,
        ValidatesOnDataErrors=True}" />

In my RichTextBoxAssistant class, I have this dependency property:

public static readonly DependencyProperty BoundDocument =
    DependencyProperty.RegisterAttached(
        "BoundDocument",
        typeof(string),
        typeof(RichTextBoxAssistant),
        new FrameworkPropertyMetadata(
            null,
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            boundDocumentChanged
        )
    );

The problem is that the boundDocumentChanged method gets hit when I change the value in my first RichTextBox, rtb_description, but not when I change the value in rtb_descriptionHowTo. When I change the text or do anything to rtb_descriptionHowTo, I never reach boundDocumentChanged. Is this a result of RichTextBoxAssistant being a static class? How can I fix it so that I can use RichTextBoxAssistant with multiple RichTextBoxes?

4
  • 2
    Does the event on the second rich text box fire if you remove the binding on the first?
    – ColinE
    Dec 20, 2010 at 22:15
  • ColinE: good question. It doesn't, so now I'm looking into other issues. Bet it's not RichTextBoxAssistant causing the problem after all! Dec 20, 2010 at 22:21
  • could it be that DescriptionHowTo is not a DependencyProperty?
    – Vlad
    Dec 20, 2010 at 22:21
  • Vlad: DescriptionHowTo is not a DependencyProperty, but its setter does call PropertyChanged (it's in a view model). Dec 21, 2010 at 18:35

1 Answer 1

0

ColinE had the right idea: I tried removing the attached property from my first RichTextBox, and changing the value of the second RichTextBox still didn't put me in boundDocumentChanged. Turns out my DescriptionHowTo property in my view model was null, instead of an HTML string. When I initialized it to @"<html><body></body></html>", things started working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.