1

First I will explain the context of the problem, because you might be able to point me in a better direction.

I need to implement a undo-redo like system on an object. The object has a series of dependency properties. Some are double, int, string but some are also of DependencyObject type. I need to save the value of the property before it is changed, and for this I added the CoerceValueCallback.

public static readonly DependencyProperty MyBackgroundProperty =
        DependencyProperty.Register("MyBackground", typeof(MyCustomizableBackground),
            typeof(MyComponent), new UIPropertyMetadata(default(MyCustomizableBackground), null, new CoerceValueCallback(OnPropertyChanging)));

In OnPropertyChanging I save the value before it's changed. MyCustomizableBackground is the DependencyObject that has also some dependency properties.

The problem is that in this case, where I have a custom object as a property, the OnPropertyChanging method isn't triggered, but when I have a common type, it is triggered.

Later edit: I realised that a part of my question was quite ambiguous and I asked a separate question here. For the first part of the problem, Julien pointed me in a better direction.

Community
  • 1
  • 1
morsanu
  • 975
  • 20
  • 35

1 Answers1

1

The property changed callback is the second parameter of the constructor in your case, not the third as you used which is the value coercion callback.

Edit: in response to your comment, double check that MyComponent is the good type owning the property. I remember having a similar problem a while ago after copy/pasting a DP and forgetting to change the owning type.

Julien Lebosquain
  • 40,639
  • 8
  • 105
  • 117
  • Yes, you are right, the name of my method is inappropriate but I'm looking to save the value before the change. So that's why I use CoerceValueCallback. I changed the name of the method. – morsanu May 17 '10 at 11:07
  • Yes, but PropertyChanged gives you OldValue and NewValue in the parameters so that's exactly what you need. A call to Coerce doesn't mean the value has changed. – Julien Lebosquain May 17 '10 at 11:31
  • You are right again, you should have explained this from the beginning. But the problem persists, my custom object property change doesn't call the method. – morsanu May 17 '10 at 11:49