Custom Command by KeyGesture and RoutedUICommand
<Window x:Class="Commands.CustomCommand"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Commands" Height="300" Width="300" xmlns:local="clr-namespace:Commands">
<Window.CommandBindings>
<CommandBinding Command="local:DataCommands.MyCommand" Executed="MyCommand"/>
</Window.CommandBindings>
<Grid>
<Button Margin="5" Command="local:DataCommands.MyCommand">Requery</Button>
</Grid>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Namespace Commands
Public Partial Class CustomCommand
Inherits System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub MyCommand(sender As Object, e As ExecutedRoutedEventArgs)
Console.WriteLine("Control R pressed")
End Sub
End Class
Public Class DataCommands
Shared my As RoutedUICommand
Shared Sub New()
Dim inputs As New InputGestureCollection()
inputs.Add(New KeyGesture(Key.R, ModifierKeys.Control, "Ctrl+R"))
my = New RoutedUICommand("Requery", "Requery", GetType(DataCommands), inputs)
End Sub
Public Shared ReadOnly Property MyCommand() As RoutedUICommand
Get
Return my
End Get
End Property
End Class
End Namespace
Related examples in the same category