Mvvm在模型中嵌套了属性

时间:2012-04-30 22:07:58

标签: c# mvvm model nested inotifypropertychanged

我使用包含模型类B列表的模型类A.每个模型(A和B)都实现了INotifyPropertyChanged,并且当属性发生变化时,事件会被正确触发。模型A已从模型B订阅此事件,如果模型A发生模型,则触发另一个属性更改。

我的问题是我想要在服务类中监听模型A中的更改(包括nestend模型属性),该服务类应该将更改写入数据库。但是当模型A触发propertychanged时,在我的服务类中,我无法知道已更改的属性是属于模型A还是属于模型B之一。

public class Model B : ObservableObject
{
...
  public int Property1
  {
    get{return property1;}
    set{ this.property1 = value;
         OnPropertyChanged("Property1");  
       }
  }
} 


public class ModelA : ObservableObject
{
 ...
 ObservableCollection<ModelB> modelsB;
 ...

 public ObservableCollection<ModelB> ModelsB
 {
   get
   {
     if(modelsB == null)
     {
       this.modelsB = new ObservableCollection<ModelB>();
       this.modelsB.CollectionChanged+= ModelBListChanged;
     }
     return modelsB;
   }
 }


  private int property2;
  public int Property2
  {
    get{return property2;}
    set{ this.property2 = value;
         OnPropertyChanged("Property2");  
       }
  }

 private void ModelBListChanged(object sender, NotifyCollectionChangedEventArgs args)
 {
    if(e.NewItems != null)
      {
        foreach(ObservableObject item in e.NewItems)
        {
          item.PropertyChanged += NotifyPropertyChanged;
        }
      }
      if (e.OldItems != null)
      {
        foreach (ObservableObject item in e.OldItems)
        {
          item.PropertyChanged -= NotifyPropertyChanged;
        }
      }
 }

 ...

}


public class SomeService
{
   ...

  ObservableCollection<ModelA> modelsA;

  public ObservableCollection<ModelA> ModelsA
  {
     get
     {
       if(modelsA == null)
       {
         modelsA = new ObservableCollection<ModelA>();

             //fill with data...
         ....
         foreach(...)
         {
        ModelB mb = new ModelB();
            //fill mb ...
        ...

        mb.PropertyChanged += ModelBChanged;
        modelsA.ModelsB.Add(mb);
         }
         ....
         modelsA.PropertyChanged += ModelsAChanged;
       }
       return modelsA;
     }
  }

  ...

  private void ModelsAChanged(object sender, PropertyChangedEventArgs args)
  {
    // determine the property that has changed and call the correct
    // update function for this property to write the data to database.

var ma = (ModelA) sender;

      switch(args.PropertyName)
      {
     ...
        case ModelA.Property1:
          //update database with function for property 1
          break;
        case ModelA.Property2:
          //update database with function for property 2
          break;
     ...
      }
  }

  private void ModelBChanged(object sender, PropertyChangedEventArgs args)
  {
    // How do I know to which ModelA this ModelB sender belongs?

var mb = (ModelB) sender;
switch(args.PropertyName)
      {
     ...
        case ModelB.Property1:
          //update database with function for property 1 of model B
          break;
       ...
      }
  }
}

怎么能解决这个问题?

此致

tabina

1 个答案:

答案 0 :(得分:1)

我不确定您当前的代码是否编译?您正在将B集合中的CollectionChanged事件转换为A类上的PropertyChanged事件,但您引用PropertyName中不存在的NotifyCollectionChangedEventArgs。< / p>

即使你可以让它工作,但由于CollectionChanged事件可以引用多个项目,因此PropertyChanged只引用一个项目,因此没有多大意义。

您的B系列已经是A的公共财产。为什么您不能直接订阅服务中的更改?例如

public class SomeService
{
    ObservableCollection<ModelA> modelsA;

    public ObservableCollection<ModelA> ModelsA
    {
        get
        {
            if(modelsA == null)
            {
               modelsA = new ObservableCollection<ModelA>();
               //fill with data...

               modelsA.CollectionChanged += ModelsAChanged;

               foreach (ModelA A in modelsA)
                   A.ModelsB.CollectionChanged += ModelsBChanged;
            }

            return modelsA;
        }
    }

    private void ModelsAChanged(object sender, NotifyCollectionChangedEventArgs args)
    {
        //remember to subscribe to ModelsB in any new ModelA instance that get added 
        //to the modelsA collection too, and unsubscribe when they get removed.
    }

    private void ModelsBChanged(object sender, NotifyCollectionChangedEventArgs args)
    {

    }
}