C#/WPF:ViewModel 中所有属性的 PropertyChanged?
我有一个这样的类:
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;
}
我的文本框绑定到 ViewModel 的名称和年龄。 如果我更改 ViewModel 中的 _person 对象,是否必须再次为每个属性调用 RaisePropertyChanged 还是有办法自动执行此操作(在我的具体示例中,我有大约 15 个属性..)?
感谢您的任何帮助。
干杯 约瑟夫
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..)?
Thanks for any help.
Cheers
Joseph
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
null
或string.Empty
作为PropertyChangedEventArgs
中的属性名称来指示所有属性已更改。 PropertyChanged。You can indicate all properties have changed by using
null
orstring.Empty
for the property name inPropertyChangedEventArgs
. This is mentioned in the documentation for PropertyChanged.我用来解决以下问题的另一种解决方案:首先设置值,然后调用
PropertyChangedEventArgs
是在我的ViewModelBase
中添加Set
函数看起来像这样:而不是这样做:
您现在可以通过这样做来实现相同的目的:
One other solution I used to tackle the problem of: first setting the value and then calling the
PropertyChangedEventArgs
is by adding aSet
function in myViewModelBase
which looks like this:Instead of doing this:
You can now achieve the same by doing this: