You can create your own custom RoutedCommand
, reimplement ICommand
and register an additional event yourself. Note that using new
is supposedly safe here because WPF will use an ICommand
reference to 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 be aware that while WPF will use a WeakEventManager
to register to this event, others devs might not. Since routed commands are usually defined as static fields, this could be a potential memory leak. You could implement a weak event manager to prevent this.