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)

Syntax

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

Type Parameters

  • T
    Parameter type.

Remarks

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.

Examples

            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

System.Object
  Microsoft.Practices.Prism.Commands.DelegateCommandBase
    Microsoft.Practices.Prism.Commands.DelegateCommand<T>

See Also

DelegateCommand<T> Members

Microsoft.Practices.Prism.Commands Namespace