I want to move to the next control when I press the Enter key instead of the Tab key in a WPF MVVM application. How can I achieve this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you only want it to work for a few text boxes, Jay’s answer is best.
If you want your whole application to work that way, makwana.a’s answer is better but can be improved.
Below is my modification of makwana.a’s answer, which I have used in numerous applications. It also includes support for moving to the next control via enter if the active control is a check box. Instead of using the tag property to decide whether or not the focus should move, I used the
AcceptsReturnproperty of the text box. I did this because it defaults to false and will only be set to true on multi-line text boxes. In that case, you won’t want the focus to move to the next control on enter anyway.Declare these event handlers in the OnStartup void of App.xaml
Here are the rest of the methods needed to make it work application wide.
Edit
I updated the code to mark the keystroke as handled if the movement was successful and also toggle the checkbox since the key was handled and will no longer reach it.