i have view viewmodel it's datacontext. in viewmodel have observablecollection of objects:
availablecategories = new observablecollection<category>();
i can bind listview observablecollection without trouble this:
itemssource="{binding path=availablecategories}"
i have requirement wrap observablecollection in class (to aid in xml serialization in here: how rename xml attribute generated after serializing list of objects)
the wrapper class looks this:
public class categorylist : observableobject { private observablecollection<category> _categories; public observablecollection<category> categories { { return _categories; } set { if (_categories == value) { return; } _categories = value; raisepropertychanged(()=>categories); } } }
and gets created in vm this:
categorylist cl = new categorylist(); cl.categories = new observablecollection<category>();
how bind collection within wrapper class in vm? doesn't seem work:
itemssource="{binding cl.categories}"
; edit: vm exposes categorylist this:
private categorylist _cl; public categorylist cl { { return _cl; } set { if (value==_cl) { return; } _cl = value; raisepropertychanged(()=>cl); } }
but still no joy.
try subscribing cl.propertychanged
event in vm, , in handler call raisepropertychanged(()=>cl)
again (you can check e.propertyname first if want, avoid many calls). if works, means view not being notified when collection property changes because you're binding subproperty.
if doesn't work either, might have subscribe categories.collectionchanged
, , raise cl
property change handler too... if works, means collection items changing (being added or removed) what's not being notified view.
but can lead over-complicated code, if want make sure these handlers correctly added , removed every time properties set...
in general, ill-advised create bindings subproperties, tends leads these notification problems.
Comments
Post a Comment