共用方式為


CommandBindingCollection.Add(CommandBinding) 方法

定義

將指定的 CommandBinding 加入至這個 CommandBindingCollection

public:
 int Add(System::Windows::Input::CommandBinding ^ commandBinding);
public int Add (System.Windows.Input.CommandBinding commandBinding);
member this.Add : System.Windows.Input.CommandBinding -> int
Public Function Add (commandBinding As CommandBinding) As Integer

參數

commandBinding
CommandBinding

將加入至集合中的繫結。

傳回

如果作業成功則為 0 (請注意,這不是已加入項目的索引)。

例外狀況

commandBindingnull

範例

下列範例會建立 , CommandBinding 並將其新增至 CommandBindingCollectionWindow

<Window x:Class="SDKSamples.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:custom="clr-namespace:SDKSamples"
    Height="600" Width="800"
    >
  <Window.CommandBindings>
    <CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}"
                    Executed="ExecutedCustomCommand"
                    CanExecute="CanExecuteCustomCommand" />
  </Window.CommandBindings>
CommandBinding customCommandBinding = new CommandBinding(
    CustomRoutedCommand, ExecutedCustomCommand, CanExecuteCustomCommand);

// attach CommandBinding to root window
this.CommandBindings.Add(customCommandBinding);
Dim customCommandBinding As New CommandBinding(CustomRoutedCommand, AddressOf ExecutedCustomCommand, AddressOf CanExecuteCustomCommand)

' attach CommandBinding to root window
Me.CommandBindings.Add(customCommandBinding)

適用於

另請參閱