wpfcallback2-way-object-databindingdependencyobject

WPF DependencyProperty: how to specify the OPPOSITE action of PropertyChangedCallback?


I'm adding a custom string DependencyProperty to a WPF UserControl that will be bound to a string property in my business object that contains rtf.

My PropertyChangedCallback works: it contains a snippet of code to use the e.NewValue rtf string to programmatically do a range.Load() on the RichTextBox nested in my UserControl, feeding it the rtf, when the DataContext changes or the business object's rtf string property changes (via INotifyPropertyChanged).

But what, or where, is the opposite callback to go back the other direction? When the inner RichTextBox loses focus after the user types/pastes their rich text, I need to run a mirror-image snippet of code to do the range.Save() to an rtf string, and that string needs to be written to the string property of the business object at that moment.

I am coming from Winforms, so I'm looking for the WPF analog of the Format/Parse pair that I'm accustomed to for round-tripping.


Solution

  •  this.SetCurrentValue(MyDependencyProperty, range.Save());
    

    this being the UserControl that owns the DP.

    Also make sure your binding Mode=TwoWay.