DelegateCommandBase.CanExecuteChanged Event

Occurs when changes occur that affect whether or not the command should execute. You must keep a hard reference to the handler to avoid garbage collection and unexpected results. See remarks for more information.

Namespace:  Microsoft.Practices.Prism.Commands
Assembly:  Microsoft.Practices.Prism (in Microsoft.Practices.Prism.dll)

Syntax

public event EventHandler CanExecuteChanged
'Declaration
Public Event CanExecuteChanged As EventHandler

Implements

ICommand.CanExecuteChanged

Remarks

When subscribing to the CanExecuteChanged event using code (not when binding using XAML) will need to keep a hard reference to the event handler. This is to prevent garbage collection of the event handler because the command implements the Weak Event pattern so it does not have a hard reference to this handler. An example implementation can be seen in the CompositeCommand and CommandBehaviorBase classes. In most scenarios, there is no reason to sign up to the CanExecuteChanged event directly, but if you do, you are responsible for maintaining the reference.

Examples

The following code holds a reference to the event handler. The myEventHandlerReference value should be stored in an instance member to avoid it from being garbage collected.

            EventHandler myEventHandlerReference = new EventHandler(this.OnCanExecuteChanged);
            command.CanExecuteChanged += myEventHandlerReference;
            

See Also

DelegateCommandBase Class

Microsoft.Practices.Prism.Commands Namespace