DelegateCommand<T> Class

An ICommand whose delegates can be attached for Execute(T) and CanExecute(T). It also implements the IActiveAware interface, which is useful when registering this command in a CompositeCommand that monitors command's activity.

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


public class DelegateCommand<T> : DelegateCommandBase
Public Class DelegateCommand(Of T) _
    Inherits DelegateCommandBase

Type Parameters

  • T
    Parameter type.


The constructor deliberately prevent the use of value types. Because ICommand takes an object, having a value type for T would cause unexpected behavior when CanExecute(null) is called during XAML initialization for command bindings. Using default(T) was considered and rejected as a solution because the implementor would not be able to distinguish between a valid and defaulted values.

Instead, callers should support a value type by using a nullable value type and checking the HasValue property before using the Value property.


            public MyClass()
                this.submitCommand = new DelegateCommand<int?>(this.Submit, this.CanSubmit);
            private bool CanSubmit(int? customerId)
                return (customerId.HasValue && customers.Contains(customerId.Value));

Inheritance Hierarchy


See Also

DelegateCommand<T> Members

Microsoft.Practices.Prism.Commands Namespace