KeyBinding 類別


KeyGesture 繫結至 RoutedCommand (或其他 ICommand 實作)。Binds a KeyGesture to a RoutedCommand (or another ICommand implementation).

public ref class KeyBinding : System::Windows::Input::InputBinding
public class KeyBinding : System.Windows.Input.InputBinding
type KeyBinding = class
    inherit InputBinding
Public Class KeyBinding
Inherits InputBinding


下列範例會建立KeyBinding , 並將它新增InputBindingCollection至根視窗的。The following example creates a KeyBinding and adds it to the InputBindingCollection of the root window.

  <KeyBinding Command="ApplicationCommands.Open"
              Gesture="CTRL+R" />
// Creating a KeyBinding between the Open command and Ctrl-R
KeyBinding OpenCmdKeyBinding = new KeyBinding(

' Creating a KeyBinding between the Open command and Ctrl-R
Dim OpenCmdKeyBinding As New KeyBinding(ApplicationCommands.Open, Key.R, ModifierKeys.Control)



KeyBinding KeyGesture將與RoutedCommand產生關聯, 例如。 ICommandA KeyBinding associates a KeyGesture with a ICommand, such as a RoutedCommand. RoutedCommandICommand WPFWPF命令系統介面的主要執行。RoutedCommand is the primary implementation of the ICommand interface for the WPFWPF commanding system . 一般而言, 當執行時KeyGesture , 會叫用命令, 雖然命令列為會進一步受到命令特定因素的影響, 例如CanExecute值。In general, when the KeyGesture is executed the command is invoked, although the command behavior is further influenced by command-specific factors such as the CanExecute value. 如需命令的詳細資訊, 請參閱命令總覽For more information on commanding, see the Commanding Overview.

除了函式索引鍵和數位鍵台按鍵以外, 有效KeyGesture的必須只包含一個Key和一或多個ModifierKeysWith the exception of the function keys and the numeric keypad keys, a valid KeyGesture must contain exactly one Key and one or more ModifierKeys. 功能鍵和數位鍵臺上鍵不需要輔助按鍵, 就能成為有效KeyGesture的。Function keys and numeric keypad keys do not require a modifier key in order to be a valid KeyGesture. 您可以透過KeyGesture KeyBinding 或程式碼,指定不正確和具有無效XAMLXAML相關手勢的。It is possible to specify an invalid KeyGesture and a KeyBinding with an invalid associated gesture, either through XAMLXAML or code. 例如, 沒有任何驗證可防止建立和KeyGesture系結僅包含 nonfunction 索引鍵的, 或只有修飾詞, 但沒有索引鍵。For instance, there is no validation that prevents creating and binding a KeyGesture that contains only a nonfunction key, or only modifiers but no key. 這種KeyBinding情況永遠不會嘗試叫用其相關聯的命令。Such a KeyBinding will never attempt to invoke its associated command.

KeyBindingKeyGestureExtensible Application Markup Language (XAML)Extensible Application Markup Language (XAML)定義時, 有兩種方式可指定。When defining a KeyBinding in Extensible Application Markup Language (XAML)Extensible Application Markup Language (XAML) there are two ways to specify the KeyGesture. KeyBinding在中XAMLXAML建立的第一種方式是KeyBinding定義Gesture元素的屬性, 這可讓語法將索引鍵和修飾詞指定為單一字串, 例如 "CTRL + P"。The first way to establish a KeyBinding in XAMLXAML is to define the Gesture attribute of the KeyBinding element, which enables a syntax to specify keys and modifiers as a single string, for example "CTRL+P". 第二種方式是定義Key屬性ModifiersKeyBinding元素的屬性。The second way is to define the Key attribute and the Modifiers attributes of the KeyBinding element. 這兩種設定的KeyGesture方式都是相等的, 而且會修改相同的基礎物件, 但如果兩者都使用, 則會發生衝突。Both ways of setting the KeyGesture are equivalent and modify the same underlying object, but there will be a conflict if both are used. 在所有設定、和Key Gesture屬性Modifiers的情況下, 最後定義的屬性會用於KeyGestureIn the case when the Key, Modifiers, and the Gesture attributes are all set, the attribute which is defined last will be used for the KeyGesture. 您可能有一些情況, 例如, 最後Key一個設定只會覆寫前述手勢的主要元件, 但是會讓手勢的修飾詞相同。You can potentially have situations where for example a Key set last will overwrite just the Key component of a preceding Gesture but leave the Gesture's modifiers the same. 一般情況下, 建議您只使用的Gesture XAMLXAML屬性, 這可避免發生不明確的情況、提供最有效率的語法, 並提供最直接的序列化標記法。In general, it is recommended that you use only the Gesture attribute from XAMLXAML; this will avoid ambiguity, provides the most streamlined syntax, and provides the most straightforward representation for serialization.

可以在特定物件或類別層級上定義, 方法是在CommandManager類別上RegisterClassInputBinding註冊。 KeyBindingA KeyBinding can be defined on a specific object or at the class level by registering a RegisterClassInputBinding on the CommandManager class. 定義KeyBinding的最常見方式是在控制項範本中, 藉由宣告一InputBindings或多XAMLXAMLMouseBindingKeyBinding元素, 在中設定屬性。The most typical way to define a KeyBinding is within the control template, setting the InputBindings property in XAMLXAML by declaring one or more MouseBinding or KeyBinding elements.



初始化 KeyBinding 類別的新執行個體。Initializes a new instance of the KeyBinding class.

KeyBinding(ICommand, Key, ModifierKeys)

使用指定的 KeyBinding,和指定的 ICommandKey (將轉換為 ModifierKeys),初始化 KeyGesture 類別的新執行個體。Initializes a new instance of the KeyBinding class using the specified ICommand and the specified Key and ModifierKeys which will be converted into a KeyGesture.

KeyBinding(ICommand, KeyGesture)

使用指定的 KeyBindingICommand,初始化 KeyGesture 類別的新執行個體。Initializes a new instance of the KeyBinding class using the specified ICommand and KeyGesture.



識別 Key 相依性屬性。Identifies the Key dependency property.


識別 Modifiers 相依性屬性。Identifies the Modifiers dependency property.



取得或設定與這個輸入繫結相關聯的 ICommandGets or sets the ICommand associated with this input binding.

(繼承來源 InputBinding)

取得或設定特定命令的命令特有資料。Gets or sets the command-specific data for a particular command.

(繼承來源 InputBinding)

取得或設定命令的目標項目。Gets or sets the target element of the command.

(繼承來源 InputBinding)

取得包裝此執行個體之 CLRCLR 型別的 DependencyObjectTypeGets the DependencyObjectType that wraps the CLRCLR type of this instance.

(繼承來源 DependencyObject)

取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(繼承來源 DispatcherObject)

取得或設定與這個 KeyBinding 關聯的筆勢。Gets or sets the gesture associated with this KeyBinding.


取得值,這個值表示此執行個體目前是否已密封 (唯讀)。Gets a value that indicates whether this instance is currently sealed (read-only).

(繼承來源 DependencyObject)

取得或設定與這個 Key 關聯 KeyGestureKeyBindingGets or sets the Key of the KeyGesture associated with this KeyBinding.


取得或設定與這個 ModifierKeys 關聯 KeyGestureKeyBindingGets or sets the ModifierKeys of the KeyGesture associated with this KeyBinding.



判斷呼叫的執行是否可以存取這個 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(繼承來源 DispatcherObject)

清除屬性的區域數值。Clears the local value of a property. 要清除的屬性是由 DependencyProperty 識別項所指定。The property to be cleared is specified by a DependencyProperty identifier.

(繼承來源 DependencyObject)

清除唯讀屬性的區域數值。Clears the local value of a read-only property. 要清除的屬性是由 DependencyPropertyKey 所指定。The property to be cleared is specified by a DependencyPropertyKey.

(繼承來源 DependencyObject)

複製所指定物件之屬性的基底 (非動畫) 值。Copies the base (non-animated) values of the properties of the specified object.

(繼承來源 InputBinding)

複製指定的物件目前的屬性值。Copies the current values of the properties of the specified object.

(繼承來源 InputBinding)

強制轉型所指定相依性屬性的值。Coerces the value of the specified dependency property. 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject.

(繼承來源 DependencyObject)

建立 KeyBinding 的執行個體。Creates an instance of a KeyBinding.


判斷提供的 DependencyObject 和目前的 DependencyObject 是否相等。Determines whether a provided DependencyObject is equivalent to the current DependencyObject.

(繼承來源 DependencyObject)

使用基底 (非動畫) 屬性值,將執行個體設為所指定 Freezable 的凍結複製品。Makes the instance a frozen clone of the specified Freezable by using base (non-animated) property values.

(繼承來源 InputBinding)

將目前執行個體設為所指定 Freezable 的凍結複製品。Makes the current instance a frozen clone of the specified Freezable. 如果物件具有動畫相依性屬性,則會複製這些屬性的目前動畫值。If the object has animated dependency properties, their current animated values are copied.

(繼承來源 InputBinding)

取得這個 DependencyObject 的雜湊碼。Gets a hash code for this DependencyObject.

(繼承來源 DependencyObject)

建立特定的列舉值,以判斷哪些相依性屬性在此 DependencyObject 上具有本機設定的值。Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject.

(繼承來源 DependencyObject)

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)

傳回 DependencyObject 的這個執行個體上之相依性屬性的目前有效值。Returns the current effective value of a dependency property on this instance of a DependencyObject.

(繼承來源 DependencyObject)

重新評估指定相依性屬性的有效值。Re-evaluates the effective value for the specified dependency property.

(繼承來源 DependencyObject)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

每當這個 DependencyObject 上任何相依性屬性的有效值已更新時叫用。Invoked whenever the effective value of any dependency property on this DependencyObject has been updated. 已變更的特定相依性屬性會在事件資料中報告。The specific dependency property that changed is reported in the event data.

(繼承來源 DependencyObject)

傳回相依性屬性的區域值 (如果存在)。Returns the local value of a dependency property, if it exists.

(繼承來源 DependencyObject)
SetCurrentValue(DependencyProperty, Object)

設定相依性屬性的值,而不需要變更其值來源。Sets the value of a dependency property without changing its value source.

(繼承來源 DependencyObject)
SetValue(DependencyProperty, Object)

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。Sets the local value of a dependency property, specified by its dependency property identifier.

(繼承來源 DependencyObject)
SetValue(DependencyPropertyKey, Object)

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property.

(繼承來源 DependencyObject)

傳回值,這個值表示序列化程序是否應該序列化所提供相依性屬性的值。Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property.

(繼承來源 DependencyObject)

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(繼承來源 DispatcherObject)