heltonbiker
heltonbiker

Reputation: 27575

MVVM: how to notify the change of every object property when I change the object itself?

I have a ViewModel that contains a (Person)Patient property. In my View, I bind to some of the properties of Patient, say, Name and Age.

The problem is: if I change Patient, nothing happens in my view, unless I explicitly notify of each property change (I am using Caliburn.Micro thus the PropertyChangedBaseand NotifyOfPropertyChange stuff):

public class PersonViewModel : PropertyChangedBase {

    Person _patient;

    public Person Patient {
        get { return _patient; }
        set {
            _patient = value;
            NotifyOfPropertyChange(() => Patient); // this doesn't update the view
            NotifyOfPropertyChange(() => Name); // this updates, but would I need one line for each property??
        }
    }
}

The broader application context is this: I have a PersonManager screen, with three regions, each with their view and viewmodel: a list of persons, the information of a single person, and the list of medical procedures associated with each person.

I am almost sure I am missing something here. I would like to select a person in the person list, and then the regions showing person data and person procedures would update via binding, without having to manually notify each property change of the newly selected person.

Upvotes: 2

Views: 2520

Answers (1)

heltonbiker
heltonbiker

Reputation: 27575

I have solved the problem using a feature of PropertyChanged event, which is using null as a parameter:

The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.

Then, I solved my problem with:

    public Paciente Paciente {
        get { return _paciente; }
        set {
            _paciente = value;
            NotifyOfPropertyChange(null);
        }
    }

and it worked!

Upvotes: 8

Related Questions