c# - Disable selected checkbox on button click -


just started working mvvm design pattern , i'm stuck.

when application launches, have treeview populated list of objects names. i've setup ischecked binding, , works fine. i'm trying setup isenabled binding.

i want user select items in treeview wants, click 1 of 3 buttons perform action. on click, want selected items remain in treeview, disabled, user cannot perform action on items.

i'm using relaycommand class in application.

private icommandonexecute _execute; private icommandoncanexecute _canexecute;  public relaycommand(icommandonexecute onexecutemethod,      icommandoncanexecute oncanexecutemethod) {     _execute = onexecutemethod;     _canexecute = oncanexecutemethod; }  #region icommand members  public event eventhandler canexecutechanged {     add { commandmanager.requerysuggested += value; }     remove { commandmanager.requerysuggested -= value; } }  public bool canexecute(object parameter) {     return _canexecute.invoke(parameter); }  public void execute(object parameter) {     _execute.invoke(parameter); }  #endregion 

my object model class uses this

private bool _isenabled; public bool isenabled {     { return true; }     set { _isenabled = value}; } 

then within button method have

if (interfacemodel.ischecked) {     //does     myobjectname.isenabled = false; } 

and here xaml

<checkbox ischecked="{binding ischecked}" isenabled="{binding isenabled, mode=twoway}">     <textblock text="{binding myobjectname}" margin="5,2,1,2" horizontalalignment="left" /> </checkbox> 

you need setup this:

// viewmodel should implement inotifypropertychanged class viewmodel : inotifypropertychnaged {     private bool _isenabled;     public bool isenabled     {         { return _isenabled; }         set          {               _isenabled = value;              setpropertychanged("isenabled");  // add setter.         }     }      // comes inotifypropertychanged - ui listen event.     public event propertychangedeventhandler propertychanged;     private void setpropertychanged(string property)     {         if (propertychanged != null)         {             propertychanged( this, new propertychangedeventargs(property) );         }     } } 

note propertychanged comes having viewmodel implement inotifypropertychanged. notify ui, have raise event, , tell property changed (usually in setter - see above).

alternatively, if don't raw strings (i don't, personally), can use generics , expression trees this:

public void setpropertychanged<t>(expression<func<t, object>> onproperty)  {     if (propertychanged != null && onproperty.body memberexpression)      {         string propertynameasstring = ((memberexpression)onproperty.body).member.name;         propertychanged(this, new propertychangedeventargs(propertynameasstring));     } } 

where in setter can say:

public bool isenabled {         set      {          _isenabled = value;         setpropertychanged<viewmodel>(x => x.isenabled);       } } 

and it's typed, kinda nice.


Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

java.util.scanner - How to read and add only numbers to array from a text file -

iphone - Three second countdown in cocos2d -