windows phone 7 - How do I bind TwoWay between a CheckBox in a UserControl and the ViewModel in the wider scope? -
i have usercontrol has checkbox on it. when consume usercontrol on main xaml page, i'd twoway bind property on control property on viewmodel e.g.
<myusercontrol btnisblacklisted="{binding isblacklisted, mode=twoway}" /> when isblacklisted changes, i'd checkbox change , vice-versa.
here have,
public static readonly dependencyproperty btnisblacklistedproperty = dependencyproperty.register("btnisblacklisted", typeof(bool), typeof(myusercontrol), new propertymetadata(false, new propertychangedcallback(btnisblacklistedpropertychanged)) ); private static void btnisblacklistedpropertychanged(dependencyobject d, dependencypropertychangedeventargs e) { // ... here ... } public bool btnisblacklisted { { return (bool)getvalue(btnisblacklistedproperty); } set { setvalue(btnisblacklistedproperty, value); } } my usercontrol has checkbox,
<checkbox x:name="mycheckbox" ... ischecked="{binding path=btnisblacklisted, elementname=usercontrol, converter={staticresource booltonotbool}, mode=twoway}" /> the property on viewmodel object follows,
public bool isblacklisted { { return app.vm.blacklistedretailers.contains(this.retailer); } set { if (value) { app.vm.blacklistedretailers.add(this.retailer); } else { while (app.vm.blacklistedretailers.contains(this.retailer)) { app.vm.blacklistedretailers.remove(this.retailer); } } this.notifypropertychanged("isblacklisted"); } } the way blacklistedretailers changes through set method above there no need trigger notifypropertychanged there ...
i have tried many of suggestions in other questions i.e.
- using dependency property
- including
mode=twoway - binding on
usercontrolusing self-referencing datacontext set on containing grid (this not work either).
however none of these have worked.
some final notes:
- this windows phone 7.5 project
- edit: 1 way binding doe not work either, seems there problem binding
usercontrol's own properties
an elementname binding matches against x:name values in same name scope element on binding being set. there's not enough of code shown tell you're using "usercontrol" i'm guessing not set name of element, being used try , match type. elementname might not able resolve if checkbox declared inside template.
Comments
Post a Comment