c# - Close button relaycommand binding -


i trying make command binding close menu item. found called relaycommand. understand reusable.

i have following done:

the contructor of main window:

public mainwindow() {     initializecomponent();     datacontext = new classmainformbtnviewmodel();  } 

the viewmodelclass:

namespace qbox_0000.viewmodel {     public class classmainformbtnviewmodel     {         public relaycommand closeapplicatiecommand { get; private set; }          public classmainformbtnviewmodel()         {             closeapplicatiecommand = new relaycommand(closeapplicatie, canuse);         }           private void closeapplicatie(object parameter)         {             //application.current.shutdown();             messagebox.show("test");         }          public bool canuse(object parameter)         {             return true;         }     } } 

the relaycommandclass:

namespace qbox_0000 {     public class relaycommand     {         readonly action<object> _execute;         readonly predicate<object> _canexecute;          public relaycommand(action<object> execute, predicate<object> canexecute)         {             if (execute == null)             {                 throw new nullreferenceexception("execute");             }              _execute = execute;             _canexecute = canexecute;         }          public relaycommand(action<object> execute) : this(execute, null)           {          }          public event eventhandler canexecutechanged         {             add { commandmanager.requerysuggested += value; }             remove { commandmanager.requerysuggested += value; }         }           public bool canexecute(object parameter)         {             return _canexecute == null ? true : _canexecute(parameter);  //check op null en retun true         }          public void execute(object parameter)         {             _execute.invoke(parameter);         }     } } 

in view:

<menuitem x:name="menuitem_afsluiten" header="afsluiten" command="{binding closeapplicatiecommand}" commandparameter="{binding elementname=menuitem_afsluiten}" /> 

how command bind a simple menuitem closes window use of relaycommand?


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -