Question

I am using DepdendencyObjects with PropertyChangedCallbacks and I want to detect a subproperty-change inside of this callback. The proplem is that the DependencyPropertyChangedEventArgs-Object does only let me see the Property that contains the DP containing the Property that changed. Is there any way to reach this Subproperty?

class MainClass : DepencencyObject
{
    public ComplexObject MainProperty
    {
        get { return (ComplexObject)GetValue(MainPropertyProperty); }
        set { SetValue(MainPropertyProperty, value); }
    }

    public static readonly DependencyProperty MainPropertyProperty =
        DependencyProperty.Register("MainProperty", 
        typeof(ComplexObject), typeof(MainClass), 
        new PropertyMetadata(new ComplexObject([...])));

    private static void MainProperty_PropertyChangedCallback(... DependencyPropertyChangedEventArgs e)
    {
        // Unable to detect a change, if 'ComplexObject.SubProperty'
        // changes; it is shown like a change of 'MainProperty'
    }
}

class ComplexObject : DepencencyObject
{
    public int SubProperty
    {
        get { return (int)GetValue(SubPropertyProperty); }
        set { SetValue(SubPropertyProperty, value); }
    }

    public static readonly DependencyProperty SubPropertyProperty =
        DependencyProperty.Register("SubProperty", 
        typeof(int), typeof(ComplexObject), 
        new PropertyMetadata(0));
}

Someone does

(new MainClass()).MainProperty.SubProperty = 100000;

and the PropertyChangedCallback is called because MainPropertychanged (not SubProperty).

Was it helpful?

Solution

Deriving your ComplexObject, from Freezable (and implementing Freezable) should be enough, or not?

According to MSDN Documentation:

A class that derives from Freezable gains the following features:

  • Special states: a read-only (frozen) state and a writable state.
  • Thread safety: a frozen Freezable object can be shared across threads.
  • Detailed change notification: Unlike other DependencyObject objects, a Freezable object provides change notifications when sub-property values change.
  • Easy cloning: the Freezable class has already implemented several methods that produce deep clones.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top