Cimbalino Windows Phone Toolkit: AutoFocusBehavior

Most desktop applications use the Tab key to jump focus from one control to the next one, and this is expected behavior and common knowledge to most users.

On Android, the on-screen keyboard normally shows a “Next” key to – yes, you’ve guessed it! – go to the next field, and that is something really useful when filling long forms!

In truth, some native Windows Phone apps actually do this on some fields, where tapping the Enter key will have the same result, but unfortunately this is not the default behavior.

You could simulate this behavior using the TextBox.KeyUp or TextBox.KeyDown events, and then calling Control.Focus() on the next control you want to get focus, but I guess we can all agree that this is quite some work and if you change the fields order, you’d have to change the code accordingly.

The Cimbalino Windows Phone Toolkit way

Well, as of Cimbalino Windows Phone Toolkit version 3.1.0, all you need to do is add the AutoFocusBehavior to your Page and… relax! ๐Ÿ™‚

Yes, that’s really all it takes, as the behavior contains the everything needed to manage TextBox/PasswordBox control focus changes on Enter key, with some added bonus:

  • TabIndex property value from controls will be taken into account and the order will be respected
  • Any control with IsEnabled = false, Visibility = Collapsed, or TabStop = false will be skipped
  • Any TextBox control with AcceptsEnter = true will just use the default expected behavior (tapping Enter will just add a new line)
  • The AutoFocusBehavior.CycleNavigation property controls whether focus will jump from last control to first one or just focus the full page
  • The AutoFocusBehavior.SelectAllOnFocus property controls whether the entire contents of a control will be selected on focus

There is also an AfterAutoFocus event and AfterAutoFocusCommand so you can easily add some extra behavior of yours to the current one! ๐Ÿ™‚

  • Pingback: Cimbalino Windows Phone Toolkit Updated to v3.1.0 |

  • Pingback: Windows Store Developer Links – 2013-08-21 | Dan Rigby()

  • Great one! I didnt expect it to work so smooth!

  • tomcatcpt

    Firstly let me say I love using Cimbalino Toolkit for Windows Phone. I would say that its an essential Toolkit of any Windows Phone project. Its such a great productivity boost, especially for MVVM projects.

    I was very excited to see how easy the AutoFocusBehavior is able to make the enter key behave as Tab key. There is one showstopper though and from the source code its seems like it was addressed but unfortunately its not:

    When you enter a page with the AutoFocusBehavior interaction declared, it works perfectly. Now navigate away from your page to a second page. hit the back button and com back to your first page. You will notice the AutoFocusBehavior is no longer working.

    I’ve tried re-declaring it programatically but I get an InvalidOperationException. Any ideas how to keep the AutoFocusBehavior, even when you navigate back to your page?

    • You are correct, I’ve found that very same issue a week ago and already fixed it in the Cimbalino Toolkit source code (though I haven’t pushed a new NuGet package version yet!); the “old” Cimbalino Windows Phone Toolkit is still affected by this bug, I’ll be pushing the same fix for it soon!

      • tomcatcpt

        Awesome! I look forward to the update. Once again thanks for a great toolkit!

        • Cimbalino Toolkit v1.1.1 is out and has this issue properly fixed! ๐Ÿ™‚

          • tomcatcpt

            Fantastic! thank you. So will the issue also be fixed in the Phone version of the toolkit?

          • Yes, though I’m only planing to release it next week!

          • tomcatcpt

            Thanks Pedro, I really appreciate your prompt responses and efforts fix the bugs. Can’t wait for the Windows Phone Toolkit update!