WPF - Implementing System.ComponentModel.INotifyPropertyChanged for Base Class

元气小坏坏 提交于 2019-12-08 00:25:10

问题


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?


回答1:


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;
    }
}


来源:https://stackoverflow.com/questions/4289418/wpf-implementing-system-componentmodel-inotifypropertychanged-for-base-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!