InputBinding 類別

定義

表示 InputGesture 和命令之間的繫結。 命令可能是 RoutedCommand

public ref class InputBinding : System::Windows::DependencyObject, System::Windows::Input::ICommandSource
public ref class InputBinding : System::Windows::Freezable, System::Windows::Input::ICommandSource
public class InputBinding : System.Windows.DependencyObject, System.Windows.Input.ICommandSource
public class InputBinding : System.Windows.Freezable, System.Windows.Input.ICommandSource
type InputBinding = class
    inherit DependencyObject
    interface ICommandSource
type InputBinding = class
    inherit Freezable
    interface ICommandSource
Public Class InputBinding
Inherits DependencyObject
Implements ICommandSource
Public Class InputBinding
Inherits Freezable
Implements ICommandSource
繼承
繼承
衍生
實作

範例

下列範例示範如何使用 KeyBinding 將 系結 KeyGestureOpen 命令。 執行按鍵手勢時,會叫用 Open 命令。

<Window.InputBindings>
  <KeyBinding Key="B"
              Modifiers="Control" 
              Command="ApplicationCommands.Open" />
</Window.InputBindings>

下列範例示範如何將自定義命令系結至 InputBinding 物件。 這些範例會建立應用程式,讓使用者執行下列其中一個動作來變更背景色彩:

  • 按兩下按鈕。

  • 按 CTRL+C。

  • 以滑鼠右鍵按兩下 StackPanel) 外部 ListBox 的 (。

第一個範例會建立名為的 SimpleDelegateCommand類別。 這個類別接受委派,讓建立命令的物件可以定義命令執行時所發生的動作。 SimpleDelegateCommand 也會定義屬性,指定哪些按鍵和滑鼠輸入會叫用命令。 GestureKeyGestureModifier 指定鍵盤輸入; MouseGesture 指定滑鼠輸入。

 // Create a class that implements ICommand and accepts a delegate.
public class SimpleDelegateCommand : ICommand
{
    // Specify the keys and mouse actions that invoke the command. 
    public Key GestureKey { get; set; }
    public ModifierKeys GestureModifier { get; set; }
    public MouseAction MouseGesture { get; set; }

    Action<object> _executeDelegate;

    public SimpleDelegateCommand(Action<object> executeDelegate)
    {
        _executeDelegate = executeDelegate;
    }

    public void Execute(object parameter)
    {
        _executeDelegate(parameter);
    }

    public bool CanExecute(object parameter) { return true; }
    public event EventHandler CanExecuteChanged;
}
' Create a class that implements ICommand and accepts a delegate. 
Public Class SimpleDelegateCommand
    Implements ICommand

    ' Specify the keys and mouse actions that invoke the command. 
    Private _GestureKey As Key
    Private _GestureModifier As ModifierKeys
    Private _MouseGesture As MouseAction

    Public Property GestureKey() As Key
        Get
            Return _GestureKey
        End Get
        Set(ByVal value As Key)
            _GestureKey = value
        End Set
    End Property

    Public Property GestureModifier() As ModifierKeys
        Get
            Return _GestureModifier
        End Get
        Set(ByVal value As ModifierKeys)
            _GestureModifier = value
        End Set
    End Property

    Public Property MouseGesture() As MouseAction
        Get
            Return _MouseGesture
        End Get
        Set(ByVal value As MouseAction)
            _MouseGesture = value
        End Set
    End Property

    Private _executeDelegate As Action(Of Object)

    Public Sub New(ByVal executeDelegate As Action(Of Object))
        _executeDelegate = executeDelegate
    End Sub

    Public Sub Execute(ByVal parameter As Object) _
        Implements ICommand.Execute

        _executeDelegate(parameter)
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean _
        Implements ICommand.CanExecute

        Return True
    End Function

    Public Event CanExecuteChanged As EventHandler _
        Implements ICommand.CanExecuteChanged
End Class

下列範例會建立並初始化 ColorChangeCommand,也就是 SimpleDelegateCommand。 此範例也會定義叫用命令並設定 GestureKeyGestureModifierMouseGesture 屬性時所執行的方法。 應用程式會在程序開始時呼叫 InitializeCommand 方法,例如 在的 Window建構函式中。

public SimpleDelegateCommand ChangeColorCommand
{
    get { return changeColorCommand; }
}

private SimpleDelegateCommand changeColorCommand;

private void InitializeCommand()
{
    originalColor = this.Background;

    changeColorCommand = new SimpleDelegateCommand(x => this.ChangeColor(x));

    DataContext = this;
    changeColorCommand.GestureKey = Key.C;
    changeColorCommand.GestureModifier = ModifierKeys.Control;
    ChangeColorCommand.MouseGesture = MouseAction.RightClick;
}

private Brush originalColor, alternateColor;

// Switch the Background color between
// the original and selected color.
private void ChangeColor(object colorString)
{
    if (colorString == null)
    {
        return;
    }

    Color newColor = 
        (Color)ColorConverter.ConvertFromString((String)colorString);
    
    alternateColor = new SolidColorBrush(newColor);

    if (this.Background == originalColor)
    {
        this.Background = alternateColor;
    }
    else
    {
        this.Background = originalColor;
    }
}
Public ReadOnly Property ChangeColorCommand() As SimpleDelegateCommand
    Get
        Return _changeColorCommand
    End Get
End Property

Private _changeColorCommand As SimpleDelegateCommand
Private originalColor As Brush, alternateColor As Brush

Private Sub InitializeCommand()
    originalColor = Me.Background

    _changeColorCommand = New SimpleDelegateCommand(Function(x) Me.ChangeColor(x))

    DataContext = Me
    _changeColorCommand.GestureKey = Key.C
    _changeColorCommand.GestureModifier = ModifierKeys.Control
    _changeColorCommand.MouseGesture = MouseAction.RightClick
End Sub

' Switch the Background color between 
' the original and selected color. 
Private Function ChangeColor(ByVal colorString As Object) As Integer

    If colorString Is Nothing Then
        Return 0
    End If

    Dim newColor As Color = DirectCast(ColorConverter.ConvertFromString(DirectCast(colorString, [String])), Color)

    alternateColor = New SolidColorBrush(newColor)

    If Brush.Equals(Me.Background, originalColor) Then
        Me.Background = alternateColor
    Else
        Me.Background = originalColor
    End If

    Return 0
End Function

最後,下列範例會建立使用者介面。 此範例會將 和 加入KeyBindingStackPanel ,其中包含 ButtonListBoxMouseBinding 當使用者在 中 ListBox選取專案時,他們可以將背景的色彩變更為選取的色彩。 在每個案例中 CommandParameter ,屬性都會系結至 中 ListBox選取的專案,而 Command 屬性會系結至 ColorChangeCommandKeyBinding.KeyKeyBinding.ModifiersMouseBinding.MouseAction 屬性會系結至 類別上的SimpleDelegateCommand對應屬性。

<StackPanel Background="Transparent">
  <StackPanel.InputBindings>
    
    <KeyBinding Command="{Binding ChangeColorCommand}"
                CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}"
                Key="{Binding ChangeColorCommand.GestureKey}"
                Modifiers="{Binding ChangeColorCommand.GestureModifier}"/>

    <MouseBinding Command="{Binding ChangeColorCommand}"
                  CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}"
                  MouseAction="{Binding ChangeColorCommand.MouseGesture}"/>
  
  </StackPanel.InputBindings>
  
  <Button Content="Change Color" 
          Command="{Binding ChangeColorCommand}" 
          CommandParameter="{Binding ElementName=colorPicker, Path=SelectedItem}">
  </Button>

  <ListBox Name="colorPicker"
           Background="Transparent"
           xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <sys:String>Red</sys:String>
    <sys:String>Green</sys:String>
    <sys:String>Blue</sys:String>
    <sys:String>Yellow</sys:String>
    <sys:String>Orange</sys:String>
    <sys:String>Purple</sys:String>
  </ListBox>
</StackPanel>

備註

您可以藉由建立 InputBinding來指定使用者輸入叫用命令。 當使用者執行指定的輸入時, ICommand 會執行設定為屬性的 Command

您可以藉由在、 CommandParameterCommandTarget 屬性上Command建立系結,指定 叫InputBinding用 對象上定義的命令。 這可讓您定義自定義命令,並將其與使用者輸入產生關聯。 如需詳細資訊,請參閱一節中的第二個範例。

可以在特定物件或類別層級上定義 ,InputBinding方法是向 CommandManager註冊 RegisterClassInputBinding

類別 InputBinding 本身不支援 XAML 使用方式,因為它不會公開公用無參數建構函式, (有無參數建構函式,但受到保護) 。 不過,衍生類別可以公開公用建構函式,因此可以在使用 XAML 使用方式繼承自 InputBinding 的衍生類別上設定屬性。 兩個可以在 XAML 中具現化的現有 InputBinding衍生類別,而且可以在 XAML 中設定屬性為 KeyBindingMouseBinding。 WPF 程式設計中的一般屬性是在 XAML 中設定,並接受一或多個 InputBinding 物件做為值,就是 UIElement.InputBindings 屬性。

XAML 物件項目用法

<inputBindingDerivedClass.../>

XAML 值

inputBindingDerivedClass
的衍生類別 InputBinding ,支援物件項目語法,例如 KeyBindingMouseBinding。 請參閱<備註>。

建構函式

InputBinding()

提供 InputBinding 衍生類別的基底初始化。

InputBinding(ICommand, InputGesture)

使用指定的命令和輸入筆勢初始化 InputBinding 類別的新執行個體 (Instance)。

欄位

CommandParameterProperty

識別 CommandParameter 相依性屬性。

CommandProperty

識別 Command 相依性屬性。

CommandTargetProperty

識別 CommandTarget 相依性屬性。

屬性

CanFreeze

取得值,指出是否可以將物件設為不可修改。

(繼承來源 Freezable)
Command

取得或設定與這個輸入繫結相關聯的 ICommand

CommandParameter

取得或設定特定命令的命令特有資料。

CommandTarget

取得或設定命令的目標項目。

DependencyObjectType

DependencyObjectType取得包裝這個實體之 CLR 型別的 。

(繼承來源 DependencyObject)
Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)
Gesture

取得或設定與這個輸入繫結相關聯的 InputGesture

IsFrozen

取得值,該值表示物件目前是否可修改。

(繼承來源 Freezable)
IsSealed

取得值,這個值表示此執行個體目前是否已密封 (唯讀)。

(繼承來源 DependencyObject)

方法

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObject

(繼承來源 DispatcherObject)
ClearValue(DependencyProperty)

清除屬性的區域數值。 要清除的屬性是由 DependencyProperty 識別項所指定。

(繼承來源 DependencyObject)
ClearValue(DependencyPropertyKey)

清除唯讀屬性的區域數值。 要清除的屬性是由 DependencyPropertyKey 所指定。

(繼承來源 DependencyObject)
Clone()

建立這個 Freezable 的可修改複製,製作這個物件值的深層複製。 當複製這個物件的相依性屬性時,這個方法會複製運算式 (但可能已不再解析),但不會複製動畫或其目前值。

(繼承來源 Freezable)
CloneCore(Freezable)

複製所指定物件之屬性的基底 (非動畫) 值。

CloneCurrentValue()

使用 Freezable 的目前值,建立它的可修改複製品 (深層複本)。

(繼承來源 Freezable)
CloneCurrentValueCore(Freezable)

複製指定的物件目前的屬性值。

CoerceValue(DependencyProperty)

強制轉型所指定相依性屬性的值。 完成方式是叫用存在於呼叫 DependencyObject 之相依性屬性的屬性中繼資料中所指定的任何 CoerceValueCallback 函式。

(繼承來源 DependencyObject)
CreateInstance()

初始化 Freezable 類別的新執行個體。

(繼承來源 Freezable)
CreateInstanceCore()

建立 InputBinding 的執行個體。

Equals(Object)

判斷提供的 DependencyObject 和目前的 DependencyObject 是否相等。

(繼承來源 DependencyObject)
Freeze()

將目前的物件設為不可修改,並將其 IsFrozen 屬性設定為 true

(繼承來源 Freezable)
FreezeCore(Boolean)

Freezable 物件設為不可修改的,或測試是否可以將它設為不可修改的。

(繼承來源 Freezable)
GetAsFrozen()

使用基底 (非動畫) 屬性值,建立 Freezable 的凍結複本。 因為複本已凍結,所以會以傳址方式複製任何凍結子物件。

(繼承來源 Freezable)
GetAsFrozenCore(Freezable)

使用基底 (非動畫) 屬性值,將執行個體設為所指定 Freezable 的凍結複製品。

GetCurrentValueAsFrozen()

使用目前屬性值,建立 Freezable 的凍結複本。 因為複本已凍結,所以會以傳址方式複製任何凍結子物件。

(繼承來源 Freezable)
GetCurrentValueAsFrozenCore(Freezable)

將目前執行個體設為所指定 Freezable 的凍結複本。 如果物件具有動畫相依性屬性,則會複製其目前的動畫值。

GetHashCode()

取得這個 DependencyObject 的雜湊碼。

(繼承來源 DependencyObject)
GetLocalValueEnumerator()

建立特定的列舉值,以判斷哪些相依性屬性在此 DependencyObject 上具有本機設定的值。

(繼承來源 DependencyObject)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
GetValue(DependencyProperty)

傳回 DependencyObject 的這個執行個體上之相依性屬性的目前有效值。

(繼承來源 DependencyObject)
InvalidateProperty(DependencyProperty)

重新評估指定相依性屬性的有效值。

(繼承來源 DependencyObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnChanged()

目前的 Freezable 物件遭到修改時進行呼叫。

(繼承來源 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject)

確定已為剛剛設定的 DependencyObjectType 資料成員,建立適當的內容指標。

(繼承來源 Freezable)
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty)

此成員支援 Windows Presentation Foundation (WPF) 基礎結構,而且不適合直接從程式代碼使用。

(繼承來源 Freezable)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

每當這個 DependencyObject 上任何相依性屬性的有效值已更新時叫用。 已變更的特定相依性屬性會在事件資料中報告。

(繼承來源 DependencyObject)
OnPropertyChanged(DependencyPropertyChangedEventArgs)

覆寫 OnPropertyChanged(DependencyPropertyChangedEventArgs)DependencyObject 實作也可以叫用任何 Changed 處理常式,以回應類型 Freezable 的變更相依性屬性。

(繼承來源 Freezable)
ReadLocalValue(DependencyProperty)

傳回相依性屬性的區域值 (如果存在)。

(繼承來源 DependencyObject)
ReadPreamble()

確定 Freezable 是從有效的執行緒進行存取。 如果 API 會讀取非相依性屬性的資料成員,則 Freezable 的繼承者必須在該 API 的開頭呼叫這個方法。

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

設定相依性屬性的值,而不需要變更其值來源。

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

設定相依性屬性的區域值 (由相依性屬性的識別碼所指定)。

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

設定唯讀相依性屬性的區域數值 (由相依性屬性的 DependencyPropertyKey 識別項所指定)。

(繼承來源 DependencyObject)
ShouldSerializeProperty(DependencyProperty)

傳回值,這個值表示序列化程序是否應該序列化所提供相依性屬性的值。

(繼承來源 DependencyObject)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。

(繼承來源 DispatcherObject)
WritePostscript()

引發 FreezableChanged 事件,並叫用其 OnChanged() 方法。 在任何 API 修改未以相依性屬性儲存的類別成員之後,衍生自 Freezable 的類別應該在 API 的結尾呼叫這個方法。

(繼承來源 Freezable)
WritePreamble()

確認 Freezable 未凍結,而且是從有效的執行緒內容進行存取。 在任何 API 將資料寫入至非相依性屬性的資料成員之前,Freezable 繼承者應該在 API 的開頭呼叫這個方法。

(繼承來源 Freezable)

事件

Changed

發生於 Freezable 或所含的物件遭到修改時。

(繼承來源 Freezable)

適用於

另請參閱