c# - Re-assigning a new model in MVVM -> does not update child collection view models -
i have root model ~ propertyportfolio ~ save/load xml file. contains child collection of property objects.
when load application load object propertyportfolioservice. model uses inotifypropertychanged, service - i've got covered.
i have child view displays child collection of property objects in grid. binding works fine.
the problem have this:
when open new propertyportfolio file re-assign propertyportfolio object in service:
this.propertyportfolio = loadedpropertyportfolio;
the child view/viewmodel not update.
the solution have load new portfolio , recreate child objects, this:
propertyportfolio loadedpropertyportfolio = /* code load new portfolio xml */; this.propertyportfolio.properties.clear(); foreach (var property in loadedpropertyportfolio.properties) { this.propertyportfolio.properties.add(property); }
i'm looking better solution.
i hope that's descriptive enough?
further information - problem propertyviewmodel (which contains property model object plus isselected logic).
this vm individual property (for each property in collection):
public class propertyviewmodel { public property property { { return this.property; } }
public bool isselected { { return this.isselected; } set { setproperty(ref this.isselected, value, () => isselected); } } public propertyviewmodel(property property) { this.property = property; } // other code (fields etc)
}
this view model properties view (containing grid):
public class propertiesviewmodel : viewmodelbase { public observablecollection<propertyviewmodel> propertyvms { { return this.propertyvms; } } public propertiesviewmodel(propertyportfolio propertyportfolio) { func<property, propertyviewmodel> viewmodelcreator = model => new propertyviewmodel(model); this.propertyvms = new observableviewmodelcollection<propertyviewmodel, property>(propertyportfolio.properties, viewmodelcreator); } // other code (fields etc) }
Comments
Post a Comment