c# - WPF binding between ViewModel and Model -
after major edit quesiton, i'm hoping it's clear.
i'm lost binding in wpf when 1 alter should impact multiple properties.
i regularly utilize vvm bind viewmodel view , i'm ok it.
i trying implement state controller. means that, ever settings made in part of ui, reflection through out.
for illustration in part of ui, can toggle feature on or off, such "show images"
when create change, i'd in application notified , deed accordingly.
so, statecontroller class have property
public bool showimages
and in view, i'd have like
<image visible ="{binding showimages", converter={staticconverter convertme}}" />
the problem have how go making statecontroller alert of viewmodels of this.
currently, in each viewmodel i'm assuming i'd have have same property repeated
public bool showimages
eg
public class statecontroller : baseviewmodel { public bool showimages{get;set;}//imagine implementation here } public class viewmodelb : baseviewmodel { public bool showimages{}//imagine implementation here } public class viewmodelb : baseviewmodel { public bool showimages{}//imagine implementation here }
so, question is, if updated viewmodelb.showimages, how first inform statecontroller in turn updates viewmodels.
is inotifypropertychanged can automatically me since share same propertyname, or have implement logic manually, eg
public static class statecontroller { public bool showimages{get;set;}//imagine implementation here } public class viewmodela : baseviewmodel { public bool showimages { { homecoming statecontroller.showimages; } set { statecontrollershowimages = value; onpropertychanged("showimages"); } } } public class viewmodelb : baseviewmodel { public bool showimages { { homecoming statecontroller.showimages; } set { statecontrollershowimages = value; onpropertychanged("showimages"); } } }
i hate thought of above implementation show i'm trying achieve. hope there improve way!
the propertychange notification raised 1 object model.
so raising alter notification of "name"
property of classa
update ui in cases it's bound specific classa.name
. won't trigger alter notification classb.name
, or other instances of classa.name
.
i suggest using singleton here statemodel
, , having other models subscribe statemodel.propertychanged
event know if should update, this answer.
public viewmodela { public viewmodela() { statecontroller.instance.propertychanged += statecontroller_propertychanged; } void statecontroller_propertychanged(object sender, notifypropertychangedeventargs e) { // if singleton's showimages property changed, raise alter // notification class's showimages property if (e.propertyname == "showimages") onpropertychanged("showimages"); } public bool showimages { { homecoming statecontroller.instance.showimages; } set { statecontroller.instance.showimages = value; } } }
c# wpf mvvm
No comments:
Post a Comment