How do I subscribe to PropertyChanged event in my ViewModel?

katit picture katit · Oct 19, 2011 · Viewed 48.8k times · Source

I have core functionality encapsulated in ViewModelBase

Now I want to see when PropertyChanged event was raised by ViewModelBase and act on it. For example, when one property was changed on ViewModelBase - I want to change property on my ViewModel

How do I achieve this?

public class MaintainGroupViewModel : BaseViewModel<MEMGroup>
    {


public abstract class BaseViewModel<T> : NotificationObject, INavigationAware
        where T : Entity
    {

Answer

Rachel picture Rachel · Oct 19, 2011

Usually I use register to the PropertyChanged event in the class Constructor

public MyViewModel()
{
    this.PropertyChanged += MyViewModel_PropertyChanged;
}

and my PropertyChanged event handler looks like this:

void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "SomeProperty":
            // Do something
            break;
    }
}