WPF - 实施System.ComponentModel.INotifyPropertyChanged为基地班 [英] WPF - Implementing System.ComponentModel.INotifyPropertyChanged for Base Class

查看:141
本文介绍了WPF - 实施System.ComponentModel.INotifyPropertyChanged为基地班的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想implent对基类的属性System.ComponentModel.INotifyPropertyChanged接口,但我不太清楚如何把它挂。

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();

和在基类中我的code处理的变化:

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

如何处理在基类事件的转播,而无需调用OnPropertyChanged()在每个子类?

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

谢谢,

桑尼

Thanks,
Sonny

编辑:
行...所以我认为当HasChanged()的值发生变化,我应该叫 OnPropertyChanged(HasChanged),但我不知道如何来获取到的基类。任何想法?

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?

推荐答案

这是你所追求的?

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

}

注意OnPropertyChanged访问等级的保护。然后在您的具体类或子类,你做的:

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

编辑:再次读取OP的问题后,我意识到他并不想调用 OnPropertyChanged 在子类中,所以我pretty肯定这将工作:

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

在子类:

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

这篇关于WPF - 实施System.ComponentModel.INotifyPropertyChanged为基地班的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆