ICommandSource ICommandSource ICommandSource ICommandSource Interface


定义一个知道如何调用命令的对象。Defines an object that knows how to invoke a command.

public interface class ICommandSource
public interface ICommandSource
type ICommandSource = interface
Public Interface ICommandSource


命令源定义该特定对象调用命令的方式。The command source defines how a command is invoked by that particular object. 例如, 如果Button与命令相关联, 则在单击时调用Button该命令。For example, if a Button is associated with a command, the command is invoked when the Button is clicked.

如果与命令源关联的命令无法在当前命令目标上执行, 则该命令源通常会禁用自身。A command source will normally disable itself if the command it is associated with cannot execute on the current command target. 例如, 当MenuItem Paste命令无法在当前Paste命令目标上执行时, 与命令相关联的会变灰。For example, a MenuItem associated with the Paste command will gray itself out when the Paste command cannot execute on the current command target.

通常, 命令源会侦听命令上的CanExecuteChanged事件。Normally, a command source will listen to the CanExecuteChanged event on the command. 这会在命令目标上的条件发生更改时通知命令源, 例如失去键盘焦点。This informs the command source when conditions change on the command target, such as loss of keyboard focus. 然后, 命令源可以使用CanExecute方法查询命令。The command source can then query the command using the CanExecute method.

实现WPFWPF ButtonBase MenuItem中的一些类为:、和HyperlinkICommandSourceSome of the classes in WPFWPF that implement ICommandSource are: ButtonBase, MenuItem, and Hyperlink.

在 Windows Presentation Foundation CommandTarget命令系统中, ICommandSource上的属性仅在ICommand为时才RoutedCommand适用。In the Windows Presentation Foundation commanding system, the CommandTarget property on a ICommandSource is only applicable when the ICommand is a RoutedCommand. 如果在上设置, RoutedCommand并且相应的命令不是,则忽略命令目标。ICommandSource CommandTargetIf the CommandTarget is set on a ICommandSource and the corresponding command is not a RoutedCommand, the command target is ignored.


Command Command Command Command

获取在调用命令源时执行的命令。Gets the command that will be executed when the command source is invoked.

CommandParameter CommandParameter CommandParameter CommandParameter

表示可在执行命令时传递给该命令的用户定义的数据值。Represents a user defined data value that can be passed to the command when it is executed.

CommandTarget CommandTarget CommandTarget CommandTarget

在其上执行该命令的对象。The object that the command is being executed on.