Creating a KeyBinding between the Open command and Ctrl-R : Key Event « Windows Presentation Foundation « VB.Net






Creating a KeyBinding between the Open command and Ctrl-R

Creating a KeyBinding between the Open command and Ctrl-R
    



<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandHandlerProcedural"
    >
</Window>
//File:Window.xaml.vb

Imports System
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Input
Imports System.Windows.Media

Namespace WpfApplication1
  Public Partial Class Window1
    Inherits Window
    Public Sub New()
      InitializeComponent()

      Dim MainStackPanel As New StackPanel()
      Me.AddChild(MainStackPanel)

      Dim CommandButton As New Button()
      CommandButton.Command = ApplicationCommands.Open
      CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)"
      MainStackPanel.Children.Add(CommandButton)

      Dim OpenCmdBinding As New CommandBinding(ApplicationCommands.Open, AddressOf OpenCmdExecuted, AddressOf OpenCmdCanExecute)

      Me.CommandBindings.Add(OpenCmdBinding)

      Dim OpenCmdKeyBinding As New KeyBinding(ApplicationCommands.Open, Key.R, ModifierKeys.Control)

      Me.InputBindings.Add(OpenCmdKeyBinding)
    End Sub

    Private Sub OpenCmdExecuted(target As Object, e As ExecutedRoutedEventArgs)
      MessageBox.Show("The command has been invoked.")
    End Sub

    Private Sub OpenCmdCanExecute(sender As Object, e As CanExecuteRoutedEventArgs)
      e.CanExecute = True
    End Sub
  End Class
End Namespace

   
    
    
    
  








Related examples in the same category

1.Command Handler Key BindingCommand Handler Key Binding
2.Use Keyboard.Focus to set the focus to a Text FieldUse Keyboard.Focus to set the focus to a Text Field
3.Provide Quick Keyboard Access to ButtonsProvide Quick Keyboard Access to Buttons
4.Reading keyboard modifiersReading keyboard modifiers
5.Reading individual key state with Keyboard.IsKeyDownReading individual key state with Keyboard.IsKeyDown
6.Use InputBinding to bind Application commends to Key eventsUse InputBinding to bind Application commends to Key events
7.Suppress Keyboard and Mouse EventsSuppress Keyboard and Mouse Events
8.Query Left / Right control keyQuery Left / Right control key
9.Is Key.CapsLock ToggledIs Key.CapsLock Toggled
10.Keyboard.IsKeyToggledKeyboard.IsKeyToggled
11.Query Number lock keyQuery Number lock key
12.Use KeyBinding to bind Key event to TextBox.InputBindingsUse KeyBinding to bind Key event to TextBox.InputBindings
13.Custom Command by KeyGesture and RoutedUICommandCustom Command by KeyGesture and RoutedUICommand
14.TextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged eventsTextBox PreviewKeyDown, PreviewKeyUp, PreviewTextInput, KeyDown, KeyUp and TextChanged events
15.StackPanel PreviewTextInput and PreviewKeyDownStackPanel PreviewTextInput and PreviewKeyDown
16.If input is not a number do not handle the key eventIf input is not a number do not handle the key event
17.RoutedEvents: Tunneled Key PressRoutedEvents: Tunneled Key Press
18.On Key Down HandlerOn Key Down Handler