WPF 中的按键事件

Keydown event within WPF

我目前在 WPF 中有一个基本的单击按钮,用户可以按下它来搜索我想添加​​一个按键事件。使用标准 VB 时,我可以 只需实现一个 e.keycode,但因为我使用的是 WPF,所以它似乎有所不同。我在下面包含了我的基本点击功能,有人可以添加它以便它也接受回车键吗?

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

按钮并非真正设计用于处理键盘输入。如果您希望按键事件触发按钮,您应该考虑使用 InputBindings 来触发 ICommandKeyBinding 和按钮都绑定到,如下所示:

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

Window 会将其 DataContext 设置为具有实现按钮行为的 ICommandMessageCommand 属性 的 ViewModel。

可以找到关于输入绑定的非常好的教程here