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.

  1. using dependency property
  2. including mode=twoway
  3. binding on usercontrol using 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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -