The following example shows how to use a KeyBinding to bind a KeyGesture to the Open command. When the key gesture is performed, the Open command is invoked.
<Window.InputBindings>
<KeyBinding Key="B"
Modifiers="Control"
Command="ApplicationCommands.Open" />
</Window.InputBindings>
The following examples show how to bind a custom command to InputBinding objects. These examples create an application that enables the user to change the background color by performing one of the following actions:
The first example creates a class named SimpleDelegateCommand. This class accepts a delegate so that the object creating the command can define the action that occurs when the command executes. SimpleDelegateCommand also defines properties that specify what key and mouse input invokes the command. GestureKey and GestureModifier specify the keyboard input; MouseGesture specifies the mouse input.
' 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
// 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;
}
The following example creates and initializes the ColorChangeCommand, which is a SimpleDelegateCommand. The example also defines the method that executes when the command is invoked and sets the GestureKey, GestureModifier, and MouseGesture properties. An application would call the InitializeCommand method when the program begins, such as in the constructor of a Window.
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
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;
}
}
Finally, the following example creates the user interface. The example adds a KeyBinding and a MouseBinding to a StackPanel that contains a Button and a ListBox. When the user selects an item in the ListBox, he or she can change the color of the background to the selected color. In each case, the CommandParameter property is bound to the selected item in the ListBox, and the Command property is bound to the ColorChangeCommand. The KeyBinding..::.Key, KeyBinding..::.Modifiers, and MouseBinding..::.MouseAction properties are bound to the corresponding properties on the SimpleDelegateCommand class.
<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>