我使用A类模型,其中列有B类模型清单。 每个模型(A和B)都采用该模型,并在适当变化时正确发射这些模型。 示范法 一家公司从B模型中同意了这一活动,如果发生这种事件,A型火力就会改变。
我的问题是,我想听一下A模式(包括坚固的模型特性)在服务类别中的变化,这一变化应当写上对数据库的改动。 但是,当模型A大火改变财产时,我无法知道改变的财产是否属于模型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