Add Keyboard shortcuts to AvalonEdit

Here is a small example on how to add your own keyboard shortcuts to an AvalonEdit.

A new shortcut is defined by adding a new input binding:

AvalonEditor.InputBindings.Add(new InputBinding(new MyCommand(), new KeyGesture(Key.D1, ModifierKeys.Control)));

The new input binding takes too parameters for the constructor. The first is the command which must implement the ICommand interface.

The second one is the KeyGesture which is basically the definition of the shortcut:

new KeyGesture(Key.D1, ModifierKeys.Control)

This defines a shortcut which executes when the user CTRL + 1.

The in command you can program the logic which will be executed:

public abstract class MyCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    /// <summary>
    /// Constructor
    /// </summary>
    public AEditorCommand()
    {

    }

    public bool CanExecute(object parameter)
    {
        return true;   
    }

    public void Execute(object parameter) 
    {
        MessageBox.Show("HelloWorld");
    }
}

In CanExecute you can implement additional logic which defines wether the command can be executed or not.



Datenschutzerklärung | Impressum