UIElement.CommandBindings 属性

定义

获取与此元素关联的 CommandBinding 对象的集合。Gets a collection of CommandBinding objects associated with this element. CommandBinding 为此元素启用命令处理,并声明命令、命令的事件和由此元素附加的处理程序之间的链接。A CommandBinding enables command handling for this element, and declares the linkage between a command, its events, and the handlers attached by this element.

public:
 property System::Windows::Input::CommandBindingCollection ^ CommandBindings { System::Windows::Input::CommandBindingCollection ^ get(); };
public System.Windows.Input.CommandBindingCollection CommandBindings { get; }
member this.CommandBindings : System.Windows.Input.CommandBindingCollection
Public ReadOnly Property CommandBindings As CommandBindingCollection

属性值

所有 CommandBinding 对象的集合。The collection of all CommandBinding objects.

示例

下面的示例使用标记将 CommandBinding 添加到窗口中。The following example adds a CommandBinding to a window using markup. 请注意,在 XAMLXAML中,不会在标记中将 CommandBindingCollection 声明为元素;集合对象由属性所采用的类型推断,并使用一个或多个 CommandBinding 元素填充 property 元素:Note that in XAMLXAML, the CommandBindingCollection is not declared in the markup as an element; the collection object is inferred by the type that the property takes, and you populate the property element with one or more CommandBinding elements:

<Window.CommandBindings>
  <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>

有关集合的 XAMLXAML 语法的详细信息,请参阅XAML 语法的详细信息。For more information about the XAMLXAML syntax for collections, see XAML Syntax In Detail.

下面的示例在代码中本质上是相同的:The following example does essentially the same thing in code:

// Creating CommandBinding and attaching an Executed and CanExecute handler
CommandBinding OpenCmdBinding = new CommandBinding(
    ApplicationCommands.Open,
    OpenCmdExecuted,
    OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);
' Creating CommandBinding and attaching an Executed and CanExecute handler
Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

Me.CommandBindings.Add(OpenCmdBinding)

注解

填充 CommandBindings 集合的另一种典型方法是以编程方式使用 CommandManager 方法。Another typical way to populate the CommandBindings collection is to use CommandManager methods programmatically.

XAML 属性元素用法XAML Property Element Usage

<object>  
  <object.CommandBindings>  
    oneOrMoreCommandBindings  
  </object.CommandBindings>  
</object>  

XAML 值XAML Values

oneOrMoreCommandBindingsoneOrMoreCommandBindings
一个或多个 CommandBinding 元素。One or more CommandBinding elements. 其中每个都应将 Command 特性设置为已知命令,并为 CanExecuteExecuted 处理程序实现设置特性。Each of these should have a Command attribute set to a known command, and attributes set for the CanExecute and Executed handler implementations. 有关更多信息,请参见CommandBindingFor more information see CommandBinding.

适用于

另请参阅