c# - How to distinguish between same controls in MVVM? -


i have 5 checkbox , in view:

<checkbox ischecked="{binding path=isselected, mode=twoway}" content="cb1"/> <checkbox ischecked="{binding path=isselected, mode=twoway}" content="cb2"/> <checkbox ischecked="{binding path=isselected, mode=twoway}" content="cb3"/> <checkbox ischecked="{binding path=isselected, mode=twoway}" content="cb4"/> <checkbox ischecked="{binding path=isselected, mode=twoway}" content="cb5"/> 

this of code have in viewmodel:

class checkboxesviewmodel : inotifypropertychanged { public checkboxesviewmodel() {     checkboxes= new observablecollection<models.checkboxes>();     _canexecute = true; }  private bool _isselected; public bool isselected {         {         return _isselected;     }     set     {         _isselected = value;         onpropertychanged("isselected");     } }  public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) {     propertychangedeventhandler handler = propertychanged;     if (handler != null)     {         handler(this, new propertychangedeventargs(propertyname));     } }  private observablecollection<models.checkboxes> _checkboxes = new observablecollection<models.checkboxes>(); public observablecollection<models.checkboxes> checkboxes {     { return _checkboxes ; }     set     {         _checkboxes = value;         onpropertychanged("checkboxes");     } } } 

the problem when check/uncheck 1 of checkboxes affects of them.

i assume because have exact same binding, can not figure out how make code distinguish them.

i think use command , commandparameters, not seem best solution.

p.s. let me know if see wrong code - still trying learn whole mvvm thing.

you need implement icommand (google delegatecommand able treat action icommand) bind command property of checkbox in view command on view model.

viewmodel

public icommand mycommand { get; private set; } .... mycommand = new delegatecommand((value) => this.dostuff(value)); 

xaml

<checkbox command={binding mycommand} command parameter={...} /> 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -