InputBinding 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示 InputGesture 和命令之间的绑定。 此命令可能是 RoutedCommand。
public ref class InputBinding : System::Windows::DependencyObject, System::Windows::Input::ICommandSourcepublic ref class InputBinding : System::Windows::Freezable, System::Windows::Input::ICommandSourcepublic class InputBinding : System.Windows.DependencyObject, System.Windows.Input.ICommandSourcepublic class InputBinding : System.Windows.Freezable, System.Windows.Input.ICommandSourcetype InputBinding = class
    inherit DependencyObject
    interface ICommandSourcetype InputBinding = class
    inherit Freezable
    interface ICommandSourcePublic Class InputBinding
Inherits DependencyObject
Implements ICommandSourcePublic 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 还定义指定调用命令的键和鼠标输入的属性。 
              GestureKey 并 GestureModifier 指定键盘输入; 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。 该示例还定义在调用 命令并设置 GestureKey、 GestureModifier和 MouseGesture 属性时执行的方法。 应用程序会在程序开始时调用 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
最后,以下示例创建用户界面。 该示例将 和 MouseBinding 添加到KeyBindingStackPanel包含 Button 和 的 ListBox。 当用户选择 中的 ListBox项时,他们可以将背景的颜色更改为所选颜色。 在每种情况下, CommandParameter 属性都绑定到 中的 ListBox所选项,并且 属性 Command 绑定到 ColorChangeCommand。 、 KeyBinding.KeyKeyBinding.Modifiers和 MouseBinding.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 。
可以通过在 、 CommandParameter和 CommandTarget 属性上创建绑定Command来InputBinding指定 调用对 对象定义的命令。 这使你可以定义自定义命令并将其与用户输入相关联。 有关详细信息,请参阅示例部分中的第二个示例。
InputBinding可以在特定对象上或通过向 注册RegisterClassInputBindingCommandManager在类级别定义 。
类 InputBinding 本身不支持 XAML 用法,因为它不公开公共无参数构造函数 (无参数构造函数,但它) 受保护。 但是,派生类可以公开公共构造函数,因此可以在使用 XAML 用法继承的 InputBinding 派生类上设置属性。 可以在 XAML 中实例化并且可以在 XAML 中设置属性的两个现有 InputBinding派生类是 KeyBinding 和 MouseBinding。 WPF 编程中在 XAML 中设置并采用一个或多个 InputBinding 对象作为值的典型属性是 UIElement.InputBindings 属性。
XAML 对象元素用法
< inputBindingDerivedClass.../>
XAML 值
inputBindingDerivedClass
的派生类 InputBinding ,它支持对象元素语法,例如 KeyBinding 或 MouseBinding。 请参阅“备注”。
构造函数
| InputBinding() | 提供从 InputBinding 派生的类的基初始化。 | 
| InputBinding(ICommand, InputGesture) | 用指定的命令和输入笔势初始化 InputBinding 类的新实例。 | 
字段
| 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) | 
方法
事件
| Changed | 在修改 Freezable 或其包含的对象时发生。(继承自 Freezable) |