c# - Bad MVVM practice? (CommandParameter) -


in code have 3 buttons. each of them execute different. have let them execute using same command gave them different commandparameter's specify difference.

here's example of i'm talking

xaml:

            <button command="{binding updatecommand}" commandparameter="add">add client</button>              <button command="{binding updatecommand}" commandparameter="change">change client</button>             <button command="{binding updatecommand}" commandparameter="remove">remove client</button> 

viewmodel:

        public mainwindowviewmodel()         {             clients.add(new client() { name = "client 1" });             clients.add(new client() { name = "client 2" });              //updatecommand = new clientupdatecommand(this);             updatecommand = new delegatecommand(param => clientexecutecommand((string)param), param => clientcanexecutecommand((string)param));         }          public void clientexecutecommand(string param)         {             clientdialog cd;             switch(param)             {                 case "add":                     cd = new clientdialog("add client", "add client", "random user");                     cd.showdialog();                     clients.add(new client() { name = cd.nametxtbox.text });                     break;                 case "change":                     cd = new clientdialog("change client", "change client", selectedclient.name);                     cd.showdialog();                     selectedclient.name = cd.nametxtbox.text;                     break;                  case "remove":                     clients.remove(selectedclient);                     break;             }         }          public bool clientcanexecutecommand(string param)         {             if (param == "add")                 return true;             else                 return !(selectedclient == null);         } 

i want know if bad programming practice or if should create different command each button? if ok; when should not create commandparameter's?

thanks in advance answers :)

this questionable practice @ best. ask yourself: if creating methods instead of commands, name them "update" , pass update "string param", or rather have separate methods meaningful names?

your public interface becomes less clear (you need document pass "param"), make design more fragile (a typo not result in errors or warnings), introduce magic constants. overall, make code worse. bit shorter, if you're after.

if want avoid creating commands each operation (which can verbose , annoying), can use mvvm library not force to. example, caliburn.micro, have 3 methods , 3 buttons, without need mess commands , parameters.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -