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(请注意,这不是所添加项的索引)。

例外

commandBinding 为 null。

示例

以下示例创建 并将其CommandBinding添加到 CommandBindingCollection 的 。Window

<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)

适用于

另请参阅