Im implementing a simple Button
in WPF, that is bound in XAML to an ICommand
.
Whilst it works great, I've got it triggering CanExecute()
via the following code:
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
The problem is, the instantiated ICommand
is being kept alive by this event, and even after I've finished with it, CanExecute()
is continually triggered by the CommandManager
.
I supposed I've got a temporary undesirable fix in the form of a List<EventHandler>
inside the ICommand
instance, that adds each incoming value
from the add{}
, which i'm then manually unsubscribing from by just iterating it and calling CommandManager.RequerySuggested -= eventItem
, but I'm wondering if there is a better way or i'm using it wrongly?