Sonny Boy
Sonny Boy

Reputation: 8016

WPF - Implementing System.ComponentModel.INotifyPropertyChanged for Base Class

I'd like to implent the System.ComponentModel.INotifyPropertyChanged interface for a property on a base class, but I'm not quite sure how to hook it up.

Here's the signature for the property I'd like to get notifications for:

public abstract bool HasChanged();

And my code in the base class for handling the change:

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(String info)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(info));
    }
}

How do I handle the hookup of the event in the base class without having to call OnPropertyChanged() in each child class?

Thanks,
Sonny

EDIT: OK... so I think that when the value for HasChanged() changes, I'm supposed to call OnPropertyChanged("HasChanged"), but I'm not sure how to get that into the base class. Any ideas?

Upvotes: 2

Views: 2661

Answers (1)

VoodooChild
VoodooChild

Reputation: 9784

Is this what you are after?

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    //make it protected, so it is accessible from Child classes
    protected void OnPropertyChanged(String info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }

}

Notice the OnPropertyChanged accessible level is protected. And then in your concrete class or child classes, you do:

public class PersonViewModel : ViewModelBase
{

    public PersonViewModel(Person person)
    {
        this.person = person;
    }

    public string Name
    {
        get
        {
            return this.person.Name;
        }
        set
        {
            this.person.Name = value;
            OnPropertyChanged("Name");
        }
    }
}

EDIT: after reading the OP question again, I realize that he does not want to call the OnPropertyChanged in the child class, so I am pretty sure this will work:

public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private bool hasChanged = false;
    public bool HasChanged
    {
        get
        {
            return this.hasChanged;
        }
        set
        {
            this.hasChanged = value;
            OnPropertyChanged("HasChanged");
        }
    }

    //make it protected, so it is accessible from Child classes
    protected void OnPropertyChanged(String info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }
}

and in child class:

public class PersonViewModel : ViewModelBase
{
    public PersonViewModel()
    {
        base.HasChanged = true;
    }
}

Upvotes: 2

Related Questions