UIElement.CommandBindings UIElement.CommandBindings UIElement.CommandBindings UIElement.CommandBindings Property

定义

获取与此元素关联的 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 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将属性设置为已知命令, 并CanExecute为和Executed处理程序实现设置特性。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.

适用于

另请参阅