C#/WPF:ViewModel 中所有属性的 PropertyChanged?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1859946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 21:27:13  来源:igfitidea点击:

C#/WPF: PropertyChanged for all Properties in ViewModel?

c#wpfinotifypropertychanged

提问by Joseph jun. Melettukunnel

I've a class like this:

我有一个这样的课程:

public class PersonViewModel : ViewModelBase //Here is the INotifyPropertyChanged Stuff
{
    public PersonViewModel(Person person)
    {
        PersonEntity = person;
    }

    public Person PersonEntity { 
        get { return PersonEntity.Name; }
        private set { PersonEntity.Name = value; RaisePropertyChanged("PersonEntity");
    }

    public string Name { 
        get { return PersonEntity.Name; }
        set { PersonEntity.Name = value; RaisePropertyChanged("Name");
    } 
    public int Age{ 
        get { return PersonEntity.Age; }
        set { PersonEntity.Age= value; RaisePropertyChanged("Age");
    } 

    public void ChangePerson(Person newPerson)
    {
        //Some Validation..
        PersonEntity = newPerson;
    }

My TextBoxes are bound to Name and Age of the ViewModel. If I change the _person object in the ViewModel, do I have to call for each Property a RaisePropertyChanged again or is there a way to do this automaticly (in my concret example I have about 15 Properties..)?

我的文本框绑定到 ViewModel 的名称和年龄。如果我更改 ViewModel 中的 _person 对象,我是否必须再次为每个属性调用 RaisePropertyChanged 还是有办法自动执行此操作(在我的具体示例中,我有大约 15 个属性..)?

Thanks for any help.

谢谢你的帮助。

Cheers Joseph

干杯约瑟夫

采纳答案by Phil Devaney

You can indicate all properties have changed by using nullor string.Emptyfor the property name in PropertyChangedEventArgs. This is mentioned in the documentation for PropertyChanged.

您可以使用nullstring.Empty中的属性名称来指示所有属性都已更改PropertyChangedEventArgs。这在PropertyChanged的文档中有所提及。

回答by JoshuaRMS

One other solution I used to tackle the problem of: first setting the value and then calling the PropertyChangedEventArgsis by adding a Setfunction in my ViewModelBasewhich looks like this:

我用来解决以下问题的另一种解决方案:首先设置值,然后PropertyChangedEventArgs通过Set在 my 中添加一个函数来调用is,ViewModelBase如下所示:

public class ViewModelBase : INotifyPropertyChanged
{
    protected bool Set<T>(ref T backingField, T value, [CallerMemberName] string propertyname = null)
    {
        // Check if the value and backing field are actualy different
        if (EqualityComparer<T>.Default.Equals(backingField, value))
        {
            return false;
        }

        // Setting the backing field and the RaisePropertyChanged
        backingField = value;
        RaisePropertyChanged(propertyname);
        return true;
   }
}

Instead of doing this:

而不是这样做:

public string Name { 
    get { return PersonEntity.Name; }
    set { PersonEntity.Name = value; RaisePropertyChanged("Name");
} 

You can now achieve the same by doing this:

您现在可以通过执行以下操作来实现相同的目的:

public string Name { 
    get { return PersonEntity.Name; }
    set { Set(ref PersonEntity.Name,value);
}