Is it possible to get the Value of a changed property instead of only its Name when implementing INPC?

StackOverflow https://stackoverflow.com/questions/20102315

Domanda

I am using INotifyPropertyChanged so that my domain objects in a class library can notify progress of their execution methods. This is not related to WPF or DataBinding situations.

I have read the documentation about PropertyChanged event and the PropertyChangedEventArgs has only a PropertyName property:

A PropertyChanged event is raised when a property is changed on a component. A PropertyChangedEventArgs object specifies the NAME of the property that changed.

PropertyChangedEventArgs provides the PropertyName property to get the NAME of the property that changed.

Problem is: I need to get the VALUE of the changed property, not its name, so I ask:

Is there a way to get the value of a property in PropertyChanged event args?

(because if not, I think I'll have to create myself a ReportProgress event, similar to BackgroundWorker class, is that right?)

Thanks for reading.

È stato utile?

Soluzione

You can still get the value by handling PropertyChanged. Just use the name to go back and get the value.

someObject.PropertyChanged += (sender, e) => {
    var value = sender.GetType()
        .GetProperty(e.PropertyName)
        .GetValue(sender, null);
    // do something with value?
};

Check out Type.GetProperty() and PropertyInfo.GetValue().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top