开发者

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

开发者 https://www.devze.com 2023-03-11 20:41 出处:网络
i want to execute a command if the KeyDown-event occurs. This command should only be fired if the Enter-Key was pressed.

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 a开发者_StackOverflowm 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

0

精彩评论

暂无评论...
验证码 换一张
取 消