views:

39

answers:

3

I have created a dependency property of type Binary on a RichTextBox which allows me to bind to a FlowDocument which is in binary form (byte[]) within the ViewModel. This works well, the property converts to and back correctly.

Whenever the RichTextBox looses focus then the value of the dependency property is updated with the new binary representation of the FlowDocument.

My problem is that if I have been using the RichTextBox and I close the window, the RichTextBox does not lose focus and hence the dependency property is not updated with the new binary representation of the FlowDocument and therefore new changes are not commited to the database. In my ViewModel I have a method CleanUp which gets called when a ViewModel is getting ready to be disposed, where I can save the updated document.

How can I get the dependency property to update itself as the RichTextBox doesn't lose focus if the user clicks to close the window? I brainstormed the following:

  1. Tell the dependency property to update itself via a message broadcast. I am not clear on how to register a message listener within the dependency property.
  2. Query the RichTextBox directly, get the Document, convert it to a binary object manually.
  3. Get the view to move focus to a dummy control, so that the dependency property now updates itself.

What do you guys think?

Update: the on changed event for the dependency property adds a event handler which is waiting for the RichTextBox to loose focus. It is this handler that updates the dependency with its new value.

A: 

Use an UpdateSourceTrigger of "PropertyChanged"

Something like:

{Binding Path=MyProperty, 
                    Mode=TwoWay,
                    UpdateSourceTrigger=PropertyChanged}
ozczecho
This would mean that for every small change within the RichTextBox, a new object representing the binary FlowDocument would need to be created and set as the value of the BinaryFlowDocument dependency property. I tried the above as well, but the dependency property never updated, unless focus is actually lost.
Shatterling
This would probably be the way to go if I was binding directly to the Document property of the RichTextBox.
Shatterling
So what are you binding your view model to? We bind our View models to controls as above and it works a treat.
ozczecho
I am binding to the dependency property that I created BinaryFlowDocument. The RichTextBox has been extended to hold this property.
Shatterling
A: 

I had a similar problem once, the solution I used was to move the focus to a different control and I never had any problems with this.

In my case there were several editable controls in the window so I didn't have to use a dummy control.

Nir
I am going to try to move focus via an attached property as outlined in this post: http://stackoverflow.com/questions/2596757/mvvm-how-can-i-select-text-in-a-textbox/2597085#2597085
Shatterling
A: 

What's stopping you from handling the closing/closed event of the Window and moving focus or updating the binding?

Alex Paven