2012-04-30 38 views
1

我使用的模型類A包含模型類B的列表。每個模型(A和B)都實現INotifyPropertyChanged,並且在屬性更改時正確觸發事件。模型A已經從模型B訂閱了該事件,並且如果發生模型A,則觸發另一個屬性改變。模型中的Mvvm嵌套屬性已更改

我的問題是,我想聽聽模型A中的變化(包括nestend模型屬性)在一個服務類中應該將更改寫入數據庫。但是,當模型中的火災的PropertyChanged,在我服務類我不知道這是否改變了屬性屬於模型或模型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

我不知道你當前的代碼編譯,甚至?您正在將您的B集合中的CollectionChanged事件轉換爲您的A班級中的PropertyChanged事件,但您引用的PropertyName不存在於NotifyCollectionChangedEventArgs中。

即使你可以得到這個工作,它也沒有什麼意義,因爲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) 
    { 

    } 
} 
+0

你是對的。它應該是PropertyChangedEvent而不是CollectionChanged。我把它混合起來輸入這篇文章。 – tabina

+0

我更新了上面的代碼。 – tabina