Get event args in view model of a wpf program

417 views Asked by At

I have a program where I need to access the characters typed from the keyboard into the view model. I can easily obtain the KeyEventArgs in the code behind using KeyUp method, but how do I access that in the ViewModel? Please help.

1

There are 1 answers

0
beer guy On
You need the following to pass keyEventArgs to viewmodel

1. Interaction triggers in xaml because native input binding don't support keyeventargs
<textblock>
<i:interactiontriggers>
<i:action eventname ="keyup">
<multibinding command={binding somecommand, converter={staticresource eventconverter}}">
<binding ...>
<binding...>
</multibing>
</i:action>
</i:interactiontriggers>
</textblock>

2  Create a custom command  (example, relay command) where you can initialize the command like below in viewmodel

var cmd = SomeCommand;
Customcommand c = new RelayCommand<KeyEventArgs>(cmd.Execute, cmd.CanExecute);

3. In the command action, you can access the mouseeventargs or keyeventargs.

If you look up more, you'll get an idea.