Gaudreau95
Gaudreau95

Reputation: 139

Keydown event within WPF

I currently have a basic click button in WPF which the user can press in order to search I want to add a keydown event. When using standard VB I can simply implement an e.keycode, but because I'm using WPF it seems to be different. I have included my basic click function below, could someone add to it just so it accepts the enter key as well?

Private Sub BTNSearch_Click(sender As Object, e As EventArgs) Handles BTNSearch.Click


    Dim latitude As Double = Double.Parse(TXTLat.Text.Substring(0, TXTLat.Text.IndexOf(","c)))
    Dim longitude As Double = Double.Parse(TXTLong.Text.Substring(TXTLong.Text.IndexOf(","c) + 1))



    Dim location = New Microsoft.Maps.MapControl.WPF.Location(latitude, longitude)

    Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
    BingMap.Children.Add(Pin)
    Pin.Location = location

    BingMap.Center = location

    BingMap.ZoomLevel = "18"




End Sub

Upvotes: 0

Views: 335

Answers (1)

Bradley Uffner
Bradley Uffner

Reputation: 16991

Buttons are not really designed to handle keyboard input. If you want a key event to trigger the button you should consider using InputBindings to trigger an ICommand that both a KeyBinding and the button are bound to, like this:

  <Window>
    <Window.InputBindings>
        <KeyBinding Key="F5"
                    Command="{Binding MessageCommand}" />
    </Window.InputBindings>
    <Button Command="{Binding MessageCommand}">Click Me</Button>
   </Window>

The Window would have its DataContext set to a ViewModel with a MessageCommand property of an ICommand that implements the behavior of the button.

A very good tutorial on input binding can be found here.

Upvotes: 1

Related Questions