c# - How to requery 'CanExecute' of a single RoutedCommand? -
i need refresh canexecute
state of 1 or more (though far not all) routedcommand
objects.
i know you can update commands using
commandmanager.invalidaterequerysuggested();
since updates far more commands necessary, calling function performance problem in application.
my initial hope calling canexecute()
manually raise event if state changed, not case.
when looking @ reference source then, canexecutechanged
not seem accessible derived classes provide kind of extension routedcommand
class allows raising event manually.
public event eventhandler canexecutechanged { add { commandmanager.requerysuggested += value; } remove { commandmanager.requerysuggested -= value; } }
any ideas how implement such mechanism? know delegatecommand
, need routed command, don't think class can me.
you can create own custom routedcommand
, reimplement icommand
, register additional event yourself. note using new
supposedly safe here because wpf use icommand
reference call canexecutechanged
.
public class myroutedcommand : routedcommand, icommand { private event eventhandler _canexecutechanged; public void raisecanexecutechanged() { var handler = _canexecutechanged; if (handler != null) handler(this, eventargs.empty); } public new event eventhandler canexecutechanged { add { _canexecutechanged += value; base.canexecutechanged += value; } remove { _canexecutechanged -= value; base.canexecutechanged -= value; } } }
you should aware while wpf use weakeventmanager
register event, others devs might not. since routed commands defined static fields, potential memory leak. implement weak event manager prevent this.
Comments
Post a Comment