Mvvm 嵌套属性在模型中更改
本文关键字:模型 嵌套 属性 Mvvm | 更新日期: 2023-09-27 18:32:41
我使用包含模型类 B 列表的模型类 A。每个模型(A 和 B)都实现了 INotifyPropertyChanged,当属性更改时,事件会正确触发。模型 A 已从模型 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;
...
}
}
}
如何解决这个问题?
问候
塔比纳
我不确定您当前的代码是否可以编译?您正在将 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)
{
}
}