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! :)

FacebookTwitterLinkedInWhatsAppE-Mail