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_descriptionH开发者_StackOverflowowTo
. 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?
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.
精彩评论