silverlight 4.0 - Should properties of ViewModels that implement INotifyPropertyChanged automatically update the data in DataGrid control when I update them? -
i may confused purpose behind inotifypropertychanged , silverlight...
i have xaml created in designer:
<usercontrol.resources> <collectionviewsource x:key="theviewsource" d:designsource="{d:designinstance my:transferobject, createlist=true}" /> </usercontrol.resources>
as this
<canvas datacontext="{staticresource theviewsource}"> <sdk:datagrid itemssource="{binding}" name="thedatagrid"> <sdk:datagrid.columns> <!-- columns omitted post --> </sdk:datagrid.columns> </sdk:datagrid> </canvas>
and have codebehind:
private sub control_loaded(byval sender system.object, byval e system.windows.routedeventargs) handles mybase.loaded if not (system.componentmodel.designerproperties.getisindesignmode(me)) dim mycollectionviewsource system.windows.data.collectionviewsource = ctype(me.resources("theviewsource"), system.windows.data.collectionviewsource) dim myviewmodel new viewmodels.viewmodelthathasanobservablecollectionproperty() mycollectionviewsource.source = myviewmodel.theobservablecollection() end if end sub
and viewmodel looks this:
public class viewmodelbase implements componentmodel.inotifypropertychanged protected sub raisepropertychanged(byval propertyname string) raiseevent propertychanged(me, new propertychangedeventargs(propertyname)) end sub public event propertychanged(byval sender object, byval e system.componentmodel.propertychangedeventargs) implements system.componentmodel.inotifypropertychanged.propertychanged end class public class viewmodelthathasanobservablecollectionproperty inherits viewmodelbase public sub retrievecollectioncompletedhandler(byval sender object, byval e silverlightconsumableservice.retrievemycollectioncompletedeventargs) dim unobservable list(of transferobjects.thetransferobject) = e.result 'i hoping next line of code force refresh of datagrid theobservablecollection = new observablecollection(of transferobjects.transferobject)(unobservable) end sub public sub new() dim theclient new silverlightconsumableservice.silverlightconsumablesoapclient addhandler theclient.retrievemycollectioncompleted, addressof retrievecollectioncompletedhandler theclient.retrievemycollectionasync() end sub private _theobservablecollection observablecollection(of transferobjects.transferobject) public property theobservablecollection() observablecollection(of transferobjects.transferobject) return _theobservablecollection end set(byval value observablecollection(of transferobjects.transferobject)) _theobservablecollection = value raisepropertychanged("theobservablecollection") end set end property end class
the result silverlight renders datagrid no results because theobservablecollection hasn't been populated event handler yet. that's expected. hoping after asychronous call web service, setter theobservablecollection gets called, , subsequently raisepropertychanged.
my expectation silverlight handle rebinding datagrid property... isn't purpose behind using inotifypropertychanged make don't have rebind grid programmatically, or confused that?
in silverlight (unlike wpf), don't assign observablecollection in service response handler. break binding.
instead, clear out bound collection , re-populate items service response. updating collection raise notification events. if re-assign collection's reference, binding ui element broken.
the purpose behind inotifypropertychanged inform bound members value of property changed. in case of collections, re-assigning reference not count value change. updating number of collection members does. likewise, changing individual properties raise notify events long call propertychanged event. purpose of interface provide contract view listen viewmodel.
Comments
Post a Comment