wpf - Is there a way to get a callback when a control (eg: Button) completes execution of ICommand? -


i have user control takes exposes icommand dependency properties, allowing vm specify command.

is there way can attach handler (callback) in buttons of control (which execute icommand) control knows when command execution completed?

thanks interest.

you can create abstract callbackablecommand raise callback method.

abstract class callbackablecommand : icommand   {     private iinputelement getraisedelement()     {       return keyboard.focusedelement;           }      public void execute(object parameter)     {       executeimpl(parameter);       var element = getraisedelement();       if(element == null) return;        //var ci = typeof(executedroutedeventargs).getconstructors(bindingflags.nonpublic | bindingflags.instance)[0];       //var routedeventargs = (routedeventargs)ci.invoke(new object[] { this, parameter });       var routedeventargs = new routedeventargs();       //routedeventargs.routedevent = commandmanager.executedevent;       routedeventargs.routedevent = callbackable.commandexecutedevent;       routedeventargs.source = element;       routedeventargs.handled = false;        element.raiseevent(routedeventargs);                 }          public abstract void executeimpl(object parameter);      abstract public bool canexecute(object parameter);      abstract public event eventhandler canexecutechanged;   } 

inherit command callbackablecommand , override canexecute, canexecutechanged , executeimpl(instead of execute)

  class simplecommand : callbackablecommand   {     public override void executeimpl(object parameter)     {       messagebox.show("simple command execute parameter: "          + parameter ?? "null");     }      public override bool canexecute(object parameter)     {       return true;     }      public override event eventhandler canexecutechanged;   } 

own element specify commandexecuted event:

  public class callbackable : contentcontrol   {     public static readonly routedevent commandexecutedevent = eventmanager.registerroutedevent(         "commandexecuted", routingstrategy.bubble, typeof(routedeventhandler), typeof(callbackable));      // provide clr accessors event     public event routedeventhandler commandexecuted     {       add { addhandler(commandexecutedevent, value); }       remove { removehandler(commandexecutedevent, value); }     }   } 

edit: in control specify callbackable.commandexecuted event

<grid>     <grid.resources>         <local:simplecommand x:key="btncommand" />                 </grid.resources>     <local:callbackable>         <button command="{staticresource btncommand}"                    commandparameter="param"                                 local:callbackable.commandexecuted="button_executed" >             click me         </button>     </local:callbackable> </grid> 

executed event handler:

private void button_executed(object sender, executedroutedeventargs e) {   messagebox.show("executed"); } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -