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 CommandBinding , 在中, 不會在標記中宣告為專案;集合物件是由屬性所採用的類型所推斷,而您會以一或多個元素填入屬性專案:CommandBindingCollectionNote 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將屬性設定為已知的命令, 並將屬性設定為和Executed處理常式的CanExecute執行。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.

適用於

另請參閱