【问题标题】:Keydown event within WPFWPF中的Keydown事件
【发布时间】:2016-01-14 18:37:10
【问题描述】:

我目前在 WPF 中有一个基本的单击按钮,用户可以按下它来搜索我想添加​​一个 keydown 事件。使用标准 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

【问题讨论】:

    标签: wpf vb.net keydown


    【解决方案1】:

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

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

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

    一个很好的输入绑定教程可以找到here

    【讨论】:

    • 您好,感谢您的回答,我将如何添加此输入绑定?
    猜你喜欢
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 2015-09-05
    • 1970-01-01
    • 2020-07-05
    • 2015-09-05
    相关资源
    最近更新 更多