简体   繁体   中英

Easiest way to execute a command if KeyDown-event is fired in WPF MVVM with VB.NET?

i want to execute a command if the KeyDown-event occurs. This command should only be fired if the Enter-Key was pressed.

How can I realize that in a MVVM-architecture?

I am using WPF and VB.NET.

Thank you!

I don't mind letting my view handle clicks. It's there to handle user interaction right? It just has to tell the view model what to do. MVVM easy button!

Private Sub FinishButton_Click(sender As Object, e As RoutedEventArgs) Handles btnFinish.Click
    'Get your view model from your datacontext
    Dim objVM As MyViewModel = CType(Me.DataContext, MyViewModel)

   'Execute command in your view model
   objVM.FinishCommand()
End Sub

The easiest way is to implement some kind of AttachedBehavior which allows you to hook a ViewModel command to a KeyDown event.

Usually I use the code posted here for attaching VM Command's to XAML Events, however I have not tested it with a KeyDown event.

If that doesn't work, I believe other 3rd party toolkits such as Prism or MVVM Light have their own implementation of the behavior

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM