I'd like to have methodToBeCalledWhenPropertyIsSet() execute when Property in the Model is changed.
How could I do this?
If I understand correctly, I could add MyModel.PropertyChanged += methodToBeCalledWhenPropertyIsSet
somewhere in my ViewModel to subscribe to the PropertyChanged event in general but I only care when Property is set
public class ViewModel : INotifyPropertyChanged
{
...
public Model MyModel { get; set; }
public void methodToBeCalledWhenPropertyIsSet() { }
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Model : INotifyPropertyChanged
{
object _propertyField;
public object Property
{
get
{
return _propertyField;
}
set
{
_propertyField = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}