WPF - 为基类实现 System.ComponentModel.INotifyPropertyChanged

发布于 2024-10-04 20:02:58 字数 697 浏览 2 评论 0原文

我想为基类上的属性实现 System.ComponentModel.INotifyPropertyChanged 接口,但我不太确定如何连接它。

这是我想要获取通知的属性的签名:

public abstract bool HasChanged();

以及我在基类中用于处理更改的代码:

public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

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

如何处理基类中事件的连接,而不必在每个子项中调用 OnPropertyChanged()班级?

谢谢,
桑尼

编辑: 好的...所以我认为当 HasChanged() 的值发生变化时,我应该调用 OnPropertyChanged("HasChanged"),但我不确定如何将其放入基础中班级。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-10-11 20:02:58

这就是你所追求的吗?

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 可访问级别受到保护。然后在您的具体类或子类中,您执行以下操作:

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 ,所以我很确定这将起作用:

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文