I have a form that should capture KeyDown/KeyUp events.
This code fails with NRE, because it looks for KeyDown control on my current view:
this.BindCommand(ViewModel, vm => vm.KeyDown, "KeyDown");
What I've done is created wrapper class that has form as a property, so I can use this overload:
this.BindCommand(ViewModel, vm => vm.KeyDown, v => v.Form, "KeyDown");
While it works, it seems like a hack to me. Is there a proper way to bind to local events?
That's the right way to do it if you're using BindCommand. If you want to get rid of the string and you're using ReactiveUI.Events, you could also do:
this.Form.Events().KeyDown
.InvokeCommand(this, x => x.ViewModel.KeyDown);
As an aside, "KeyDown" isn't a very MVVM'y command. I'd write your key => command mappings at the View layer, like this (coding via TextArea, ignore syntax bugs):
this.Form.Events().KeyDown
.Where(x => x.Key == Key.C && (x.Modifier & Modifier.Ctrl))
.InvokeCommand(this, x => x.ViewModel.CopyText;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With