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項目: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.

適用於

另請參閱