I'm currently making a calculator app for my .NET class. The calculator works perfectly fine when I use the buttons. But when I use keys on the keyboard, it doesn't work so well. For example, when I press the Enter key, I want the calculator to calculate the problem in the text box. Instead, the enter presses the button I had last clicked which leads to issues. How can I prevent this from happening?
I know that's a odd explanation, but I don't know how to explain it better.
On the root level of your window/user control add an InputBinding
for the return keystroke. This will intercept the keystroke and execute your command as if you had clicked the button (assuming it's Command
property is also bound to that command).
Example with a UserControl:
<UserControl.InputBindings>
<KeyBinding Key="Return" Command="{Binding EvaluateCommand}"/>
</UserControl.InputBindings>
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