How does the dependency property work?

I have the following 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 Modified , which should fire when the dependency property changes. My property type is RegistrashionCardSearch (class). When I change the value of the class properties in the dependency property, the change call back property is not fired. Why ?? My RegistrashionCardSearch class implements the INotifePropertyChanged interface

+3
source share
2 answers

Ronald has already explained well why your approach is not working. To make it work, you need to subscribe to the PropertyChangedevent 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!");
}
+1
source

, . , :

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

, .

, - :

myObject.RequestObject.SomeProperty = newPropertyValue;

, RequestObject, .

+3

Source: https://habr.com/ru/post/1769297/


All Articles