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
using central event dispatching of weakeventmanager enables handlers listeners garbage collected (or manually purged) if source object lifetime extends beyond listeners.
which used in
this should solve issue of needing explicitly un-register?
Comments
Post a Comment