2

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:

enter image description here

When the user press the ENTER key on the keyboard, the focus goes to the textbox below it:

enter image description here

And now, what I want is when the user presses the Enter key while the Focus is at the last textbox, like this:

enter image description here

Then I want the focus to go to the next textbox like this:

enter image description here

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?

mecocopy
  • 187
  • 1
  • 3
  • 14
  • Have you tried using [tabindex](https://stackoverflow.com/questions/359758/setting-tab-order-in-wpf)? – Mike May 02 '18 at 08:16
  • 2
    Possible duplicate of [Setting tab order in WPF](https://stackoverflow.com/questions/359758/setting-tab-order-in-wpf) – CodeNotFound May 02 '18 at 08:43
  • have you tried using the VisualTreeHelper.GetParent from the textbox to get to the list? – dnr3 May 02 '18 at 08:52
  • I forgot to mention. I already tried the KeyboardNavigation.TabIndex but my problem is. It is triggered using TAB key. But my requirements is using ENTER key – mecocopy May 03 '18 at 01:36

1 Answers1

1

I would strongly discourage you from doing that, because normal users are used to tab | shift+tab navigation and enter key as an equivalent of the accept - button.

But if you really need to use the enter key you could still use the tabbing mechanism and send tab on AssociatedObject_PreviewKeyDown using the InputManager.

https://michlg.wordpress.com/2013/02/05/wpf-send-keys/

Gabsch
  • 471
  • 4
  • 10