c# - Simple way to create a command -


i new c# , need create simple binding command 1 button. have been reading lot of articles last hours got me more confused.

i have wpf window (let's window1) have button "addcustomer". simplest way create command it? (by simplest mean easy understand)

in every article doing differently. need show me how bind in xaml, more detailed better... said, new.

thank help.

here's take on it, following 'simplest' because leveraging prism library , amount of code write small. use nuget manager add prism project if you're not using ...

the xaml:

<button command="{binding addcustomercommand}" content="add customer"/> 

in viewmodel: (1) declare command:

public icommand addcustomercommand{ get; private set; } 

(2) define command:

addcustomercommand= new delegatecommand(addcustomer); 

(3) , create method:

private void addcustomer() {     //do logic here } 

extended version: can pass parameter xaml:

<button command="{binding addcustomercommand}" commandparameter={binding selecteditem, elementname=myselectorthingy} content="add customer"/> 

remember change signature of delegate , method:

addcustomercommand= new delegatecommand<whatevermytypeis>(addcustomer);  private void addcustomer(whatevermytypeis selectedthing) {     //do logic here } 

you can define in delegatecommand when button should available (canexecute), following:

public delegatecommand addcustomercommand{ get; private set; }  addcustomercommand = new delegatecommand(addcustomer, addcustomer_canexecute); 

and define method deciding whether can execute or not:

private bool addcustomer_canexecute() {     if (datetime.now.dayofweek.equals(dayofweek.monday))       return true;     else       return false; } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -