Auto Tab in Silverlight 3

I have a requirement to be able to automatically enter a tab from one control into the "next control" in an SL3 application. For example, a TextBox is limited to 3 characters - when you enter the third character, the focus should automatically move to the next form element (my actual use is slightly different, but this example is enough).

However, since SL automatically detects the sequence of tabs, there seems to be no way to do this separately from reverse engineering / duplicating Silverlight logic to figure out which control in the visual tree should be the next control to get focus.

Has anyone already implemented this?

+4
source share
2 answers

I was looking for a fairly generalized solution, but I managed to deal with something rather specific - it mainly uses VisualTreeHelper to find children with the same parent as the control that I want to display next to it, and focuses on it.

This is a more acceptable solution than having to go through all my controls (and this is for a fairly large LOB application) and set up the β€œnext” control for each of them.

Here is my code if it helps someone else. (VisualTreeeHelperUtil is my own class that adds some utility functions to VisualTreeHelper)

public static void TabNext(DependencyObject parentElement, Control fromControl) { var children = VisualTreeHelperUtil.FindChildren<Control>(parentElement). Where(c => c.IsEnabled && c.IsTabStop && c.Visibility == Visibility.Visible). ToList(); if (children.Contains(fromControl)) { var thisIndex = children.IndexOf(fromControl); var targetIndex = thisIndex + 1; if (children.Count > targetIndex) { var targetChild = children[targetIndex]; fromControl.Dispatcher.BeginInvoke(() => { targetChild.Focus(); var txt = targetChild as TextBox; if (txt != null) { txt.SelectAll(); } }); } } } 
+4
source

If you are looking for a generalized solution and in order, basing it on the visual ordering of the tree (as opposed to an organized layout), I think that would not be so bad. However, I have not heard about this.

Most of the forms for entering a phone number or credit card that I have seen with this behavior, honestly, just hardcode the next field in the corresponding change handler when the correct character number is entered.

Since it seems to you that your autofocus solution (for 3 characters) will already require some kind of connection to the event, tracking the TextChanged event, could you just go ahead and either 1) the Focus () hard code to the next form element which you know 2) use the Tag property to save the name of the control you want to focus on, then do FindName + Focus or 3) do some kind of VisualTreeHelper (or search for a logical tree through peers)?

+2
source

All Articles