How would you simulate a key press in MVVM in a Silverlight project?
I want to simualte the TAB key press when user presses ENTER, so it moves to the next textbox
Simply handle the KeyUp
event where you can check which key is pressed.
Then, call the Focus method of the next control.
Do not forget to set the Handled property to true
.
Sample code :
// Handler for TextBox1
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
TextBox2.Focus();
e.Handled = true;
}
}
You may also consider iterating over all controls, to find the next focusable element, using TabIndex property.
You can even wrap everything in a attachable behavior, in order to simplify the wiring up.