c# - If a model implements INotifyPropertyChanged, how should ViewModel register/deregister for PropertyChanged event? -


i have model implements inotifypropertychanged , may updated background business thread. related viewmodel implements inotifypropertychanged. , view binds viewmodel. view may shown on multiple places, , goal of them updated when model changes.

i know viewmodel should register propertychanged event of model. don't know when , best place registering , deregistering. specially deregistering, since i'm scared of having hundreds of vm event handlers on model vm/views not shown anymore.

thanks in advance.

is absolute necessity limit view not directly bind model?

you can expose model property on vm , have view directly bind thereby not having vm subscribe inpc model

something like:

public class myviewmodel: inotifypropertychanged { ...  private mymodel _model; public mymodel model {   {     return _model;   }   set {     if (value == _model)       return;     value = _model;     raisepropertychanged(() => model);   } } ...  } 

and in xaml (when myviewmodel datacontext):

<textblock text="{binding model.modelproperty}" /> 

update:

maybe of tapping propertychanged events of models in "weak" fashion

iweakeventlistener

using central event dispatching of weakeventmanager enables handlers listeners garbage collected (or manually purged) if source object lifetime extends beyond listeners.

which used in

josh smith's propertyobserver

this should solve issue of needing explicitly un-register?


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -