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

发布于 2024-08-13 23:22:11 字数 964 浏览 6 评论 0原文

我有一个这样的类:

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 技术交流群。

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

发布评论

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

评论(2

若水般的淡然安静女子 2024-08-20 23:22:11

您可以使用 nullstring.Empty 作为 PropertyChangedEventArgs 中的属性名称来指示所有属性已更改。 PropertyChanged。

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

穿越时光隧道 2024-08-20 23:22:11

我用来解决以下问题的另一种解决方案:首先设置值,然后调用 PropertyChangedEventArgs 是在我的 ViewModelBase 中添加 Set 函数看起来像这样:

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

而不是这样做:

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

您现在可以通过这样做来实现相同的目的:

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

One other solution I used to tackle the problem of: first setting the value and then calling the PropertyChangedEventArgs is by adding a Set function in my ViewModelBase which looks like this:

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