InputBinding 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表 a 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
- 繼承
- 繼承
- 衍生
- 實作
範例
以下範例展示了如何使用 a KeyBinding 將 a KeyGesture 綁定到 指令 Open 。 當執行鍵位時,會呼叫 Open 指令。
<Window.InputBindings>
<KeyBinding Key="B"
Modifiers="Control"
Command="ApplicationCommands.Open" />
</Window.InputBindings>
以下範例展示了如何將自訂指令綁定到 InputBinding 物件上。 這些範例建立了一個應用程式,讓使用者可以透過執行以下其中一項動作來更改背景顏色:
按下按鈕。
按下 CTRL+C。
右鍵點擊 a 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
最後,以下範例建立使用者介面。 範例將 a KeyBinding 和 a MouseBinding 加入 StackPanel 包含 a Button 和 ListBoxa。 當使用者在 中選擇 ListBox項目時,可以將背景顏色更改為所選顏色。 在每種情況下,屬性 CommandParameter 綁定於 中所選的項目 ListBox,而 Command 屬性則綁定於 ColorChangeCommand。 這些 KeyBinding.Key、 KeyBinding.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 的 that 會被執行。
你可以透過在 、 CommandParameter和 CommandTarget 屬性上建立綁定Command,指定 該InputBinding指令在物件上定義。 這讓你能定義自訂指令,並將其與使用者輸入關聯。 欲了解更多資訊,請參閱範例部分的第二個範例。
An InputBinding 可以定義在特定物件上,或在類別層級透過註冊 a RegisterClassInputBinding 來 CommandManager定義。
該 InputBinding 類別本身不支援 XAML 的使用 ,因為它不會暴露公開的無參數建構子(雖然有無參數建構子,但受到保護)。 然而,導出類別可以暴露公開建構子,因此可以在導出類別上設定繼承 InputBinding 自 XAML 的屬性。 兩個可 InputBinding於 XAML 實例化並在 XAML 中設定屬性的 -衍生類別為 KeyBindingMouseBinding和 。 在 WPF 程式設計中,XAML 中設定並取一個或多個 InputBinding 物件作為值的典型屬性是 屬性 UIElement.InputBindings 。
XAML 物件元素的使用
< inputBindingDerivedClass.../>
XAML 值
inputBindingDerivedClass 一個支援物件元素語法的衍生類別 InputBinding ,例如 KeyBinding 或 MouseBinding。 請參閱<備註>。
建構函式
| 名稱 | Description |
|---|---|
| InputBinding() |
提供由 InputBinding衍生的類別的基礎初始化。 |
| InputBinding(ICommand, InputGesture) |
以指定的指令和輸入手勢初始化該 InputBinding 類別的新實例。 |
欄位
| 名稱 | Description |
|---|---|
| CommandParameterProperty |
識別 CommandParameter 依賴性質。 |
| CommandProperty |
識別 Command 依賴性質。 |
| CommandTargetProperty |
識別 CommandTarget 依賴性質。 |
屬性
| 名稱 | Description |
|---|---|
| CanFreeze |
會得到一個值,表示該物件是否能被設定為不可修改。 (繼承來源 Freezable) |
| Command |
取得或設定與此輸入綁定相關的 。ICommand |
| CommandParameter |
取得或設定特定指令的指令資料。 |
| CommandTarget |
取得或設定指令的目標元素。 |
| DependencyObjectType |
會取得 DependencyObjectType 包裹此實例 CLR 類型的 。 (繼承來源 DependencyObject) |
| Dispatcher |
了解 Dispatcher 這與此 DispatcherObject 有關。 (繼承來源 DispatcherObject) |
| Gesture |
取得或設定與此輸入綁定相關的 。InputGesture |
| IsFrozen |
會得到一個值,表示該物件目前是否可修改。 (繼承來源 Freezable) |
| IsSealed |
會獲得一個值,表示該實例目前是否封存(唯讀)。 (繼承來源 DependencyObject) |
方法
事件
| 名稱 | Description |
|---|---|
| Changed |
當 Freezable 它所包含的物件被修改時,會發生這種情況。 (繼承來源 Freezable) |