Compartir a través de


ICommand Interface

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines the contract for commanding.

Namespace:  System.Windows.Input
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Public Interface ICommand
public interface ICommand

XAML Values

  • commandReference
    A reference to an ICommand implementation that describes the command logic. Typically this is done through a {Binding} reference to a data source, when using view-view model designs.

The ICommand type exposes the following members.

Methods

  Name Description
Public methodSupported by Silverlight for Windows Phone CanExecute Defines the method that determines whether the command can execute in its current state.
Public methodSupported by Silverlight for Windows Phone Execute Defines the method to be called when the command is invoked.

Top

Events

  Name Description
Public eventSupported by Silverlight for Windows Phone CanExecuteChanged Occurs when changes occur that affect whether the command should execute.

Top

Remarks

The ICommand interface enables the abstraction of a parameterized method call through its Execute method. In addition, you determine whether a method can be called prior to calling it with the CanExecute method.

Typically objects implement this interface to enable method calls on the objects through the use of XAML bindings. This is particularly useful in Model-View-ViewModel (MVVM) scenarios, as it allows models to expose commands to which controls such as buttons can be bound to without requiring additional code.

Silverlight 5 implements a limited commanding surface area, rather than an entire commanding infrastructure that is equivalent to commanding in WPF. The classes that support command-initiated user interaction are the following:

You can bind the Command property of these objects and their derived classes to instances of objects that implement ICommand. In addition, the ICommand interface exists for compatibility, which is helpful if control authors migrate WPF code-defined or particularly markup-defined command bindings for a Silverlight implementation.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.