I don't know if my title is correct.
What I am doing here is navigate to the next control when the user pressed the ENTER key.
My controls are created dynamically.
Here is my code:
<ItemsControl IsTabStop="False" ItemsSource="{Binding ListControls}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Grid.Column="0">
<dxmvvm:Interaction.Behaviors>
<ee:TabOnEnterBehavior/>
</dxmvvm:Interaction.Behaviors>
</TextBox>
<TextBox Grid.Column="1">
<dxmvvm:Interaction.Behaviors>
<ee:TabOnEnterBehavior/>
</dxmvvm:Interaction.Behaviors>
</TextBox>
<TextBox Grid.Column="2">
<dxmvvm:Interaction.Behaviors>
<ee:TabOnEnterBehavior/>
</dxmvvm:Interaction.Behaviors>
</TextBox>
<TextBox Grid.Column="3">
<dxmvvm:Interaction.Behaviors>
<ee:TabOnEnterBehavior/>
</dxmvvm:Interaction.Behaviors>
</TextBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
And this is my TabOnEnterBehavior
class which i want to pass the ListControls in order to check if the user press ENTER
on the last control
public class TabOnEnterBehavior : Behavior<System.Windows.Controls.TextBox>
{
protected override void OnAttached()
{
AssociatedObject.PreviewKeyDown += AssociatedObject_PreviewKeyDown;
}
private void AssociatedObject_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var request = new TraversalRequest(FocusNavigationDirection.Down);
request.Wrapped = true;
AssociatedObject.MoveFocus(request);
}
}
protected override void OnDetaching()
{
AssociatedObject.PreviewKeyDown -= AssociatedObject_PreviewKeyDown;
}
}
When the project is loaded, this is what it looks like:
When the user press the ENTER
key on the keyboard, the focus goes to the textbox below it:
And now, what I want is when the user presses the Enter key while the Focus is at the last textbox, like this:
Then I want the focus to go to the next textbox like this:
But i can't make it work. How can i do it?
So i am thinking of accessing the List in order to check if the FOCUS
are already in the last control. But how can i pass the List to my TabOnEnterBehavior
class without destroying the MVVM pattern?
EDIT:
i also tried the KeyboardNavigation.TabIndex="3"
but my problem is it can be triggered using tab. My requirement is ENTER
key. Is it possible to change it?