为嵌套属性实现INotifyPropertyChanged [英] Implementing INotifyPropertyChanged for nested properties

查看:151
本文介绍了为嵌套属性实现INotifyPropertyChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Person类:

I have a Person class:

public class Person : INotifyPropertyChanged
{
     private string _name;
     public string Name{
     get { return _name; }
     set {
           if ( _name != value ) {
             _name = value;
             OnPropertyChanged( "Name" );
           }
     }

     private Address _primaryAddress;
     public Address PrimaryAddress {
     get { return _primaryAddress; }
     set {
           if ( _primaryAddress != value ) {
             _primaryAddress = value;
             OnPropertyChanged( "PrimaryAddress" );
           }
     }

     //OnPropertyChanged code goes here
}

我有一个地址类:

public class Address : INotifyPropertyChanged
{
     private string _streetone;
     public string StreetOne{
     get { return _streetone; }
     set {
           if ( _streetone != value ) {
             _streetone = value;
             OnPropertyChanged( "StreetOne" );
           }
     }

     //Other fields here

     //OnPropertyChanged code goes here
}

我有一个ViewModel:

I have a ViewModel:

public class MyViewModel
{
   //constructor and other stuff here

     private Person _person;
     public Person Person{
     get { return _person; }
     set {
           if ( _person != value ) {
             _person = value;
             OnPropertyChanged( "Person" );
           }
     }

}

我有一个包含以下几行的视图:

I have a View which has the following lines:

<TextBox  Text="{Binding Person.Name, Mode=TwoWay,   
    UpdateSourceTrigger=PropertyChanged />

<TextBox  Text="{Binding Person.Address.StreetOne, Mode=TwoWay,   
    UpdateSourceTrigger=PropertyChanged />

加载视图后,两个值都会显示在确定"文本框中.

Both values show up in the text box ok when the view loads.

更改为第一个文本框将在MyViewModel中触发OnPropertyChanged( "Person" ).很好.

Changes to the first text box will fire OnPropertyChanged( "Person" ) in MyViewModel. Great.

更改为第二个文本框("Person.Address.StreetOne")不会在MyViewModel内部触发OnPropertyChanged( "Person" ).这意味着它不会调用Person对象的SET方法.不是很好.有趣的是,在Address类中调用了StreetOne的SET方法.

Changes to the second text box ("Person.Address.StreetOne") does NOT fire OnPropertyChanged( "Person" ) inside MyViewModel. Meaning it doesn't call the Person object's SET method. Not great. Interestingly the SET method of StreetOne inside the Address class is called.

如何更改Person.Address.StreetOne时如何在ViewModel中获取Person对象的SET方法??

How do I get the SET method of the Person object inside the ViewModel to be called when Person.Address.StreetOne is changed???

我是否需要整理数据以使SteetOne位于Person内而不是Address?

Do I need to flatten my data so SteetOne is inside Person and not Address??

谢谢!

推荐答案

如果要调用viewmodel SET,可以创建街道属性

if you want the viewmodel SET to be called you could create a street property

public class MyViewModel
{
  //constructor and other stuff here
  public string Street{
    get { return this.Person.PrimaryAddress.StreetOne; }
    set {
       if ( this.Person.PrimaryAddress.StreetOne!= value ) {
         this.Person.PrimaryAddress.StreetOne = value;
         OnPropertyChanged( "Street" );
       }
   }

 }

xaml

<TextBox  Text="{Binding Street, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged />

但是此解决方案有其缺点.我在项目中使用芦苇答案

but this solution has its drawbacks. i go with Reeds answer in my projects

这篇关于为嵌套属性实现INotifyPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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