2012-04-30 19 views
1

モデルクラスBのリストを含むモデルクラスAを使用します。モデル(AおよびB)のそれぞれはINotifyPropertyChangedを実装し、プロパティは変更されたときに正しく起動されます。モデルAはモデルBからこのイベントにサブスクライブしており、モデルAが発生すると別のプロパティが変更されます。モデル内のMvvmネストされたプロパティ

私の問題は、データベースに変更を書き込むサービスクラスのモデルA(ネストエンドモデルのプロパティを含む)内の変更を聴きたいということです。モデルAの火災は

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

B.

変更されたプロパティは、Aをモデル化するために属している場合、私は知ることができない私のサービスクラスでは、のPropertyChangedやモデルの一つにする場合でも、これはどのように解決されるだろうか?

よろしく、私はあなたの現在のコードもコンパイルわからない

tabina

答えて

1

? BコレクションのCollectionChangedイベントをAクラスのPropertyChangedイベントに変更していますが、NotifyCollectionChangedEventArgsに存在しないPropertyNameを参照しています。

CollectionChangedイベントは複数のアイテムを参照できますが、PropertyChangedは1つのみを参照するため、動作させることはできますが、意味がありません。

あなたの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

あなたはそうです。 CollectionChangedではなくPropertyChangedEventでなければなりません。私はそれをこの投稿をタイプミックスしました。 – tabina

+0

上記のコードを更新しました。 – tabina

関連する問題