Search code examples
wpfdependency-propertiescoerce

WPF DependencyProperty event before content changed


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.


Solution

  • 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.