views:

45

answers:

2

I have next dependency property:

    public static DependencyProperty RequestObjectProperty = DependencyProperty.Register("RequestObject", typeof(RegistrationCardSearch), typeof(RegCardSearchForm),new UIPropertyMetadata(Changed));

    private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        MessageBox.Show("Property Changed!!!");
    }
    public RegistrationCardSearch RequestObject
    {
        get
        {
            return (RegistrationCardSearch)GetValue(RequestObjectProperty);
        }
        set
        {
            SetValue(RequestObjectProperty, value);
        }
    }

and "Changed" Method which have to fire when my dependency property changes. My property type is RegistrashionCardSearch (class) . When I change property values of class in dependency property, Property changed call back not fired. Why?? My RegistrashionCardSearch class implement INotifePropertyChanged interface

+2  A: 

The changed event is fired only when the property itself changes, not when you change values inside this property. To given an example that will cause the changed event to fire:

var requestObject = myObject.RequestObject;
myObject.RequestObject = new RegistrationCardSearch() { ... };

A changed event will fire for the last line of this example because the property itself changes to another value.

However, when you do something like this:

myObject.RequestObject.SomeProperty = newPropertyValue;

the changed event will not fire because you haven't changed the RequestObject property itself, only some value inside the property.

Ronald Wildenberg
+1  A: 

Ronald already nicely explained why your approach doesn't work. To get it to work, you need to subscribe to the PropertyChanged event of your RequestObject:

private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var form = (RegCardSearchForm)d;

    if (e.OldValue != null)
        ((RegistrationCardSearch)e.OldValue).PropertyChanged -= form.RequestObject_PropertyChanged;
    if (e.NewValue != null)
        ((RegistrationCardSearch)e.NewValue).PropertyChanged += form.RequestObject_PropertyChanged;
}

private void RequestObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    MessageBox.Show("Property " + e.PropertyName + " changed!");
}
Heinzi