Are you using your datatemplate as an item template for the items container type control, e.g. a ListBox? Check out a KeyboardNavigation class, you might want to setup its TabNavigation property to "Continue" or "Cycle" for your items container, smth like this:
<ListBox x:Name="myListBox"
KeyboardNavigation.TabNavigation="Continue"
ItemTemplate="{StaticResource myDataTemplate}"
...
when focus is changed using the tab key within the ListBox, focus will move from each element and when the last element is reached focus will return to the first element for "Cycle" or move to next focusable control on the form if "Continue" is set up.
hope this helps, regards
edit0: make text box receive focus right after listbox item gets selected
<ListBox x:Name="myListBox"
KeyboardNavigation.TabNavigation="Continue"
ItemTemplate="{StaticResource myDataTemplate}"
SelectionChanged="testList_SelectionChanged"
...
/>
private void testList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(() =>
{
ListBoxItem item = testList.ItemContainerGenerator.ContainerFromIndex(testList.SelectedIndex) as ListBoxItem;
if (item != null)
{
TextBox textBox = GetDescendantTextBox(item) as TextBox;
if (textBox != null) textBox.Focus();
}
}));
}
public static Visual GetDescendantTextBox(Visual element)
{
if (element == null) return null;
if (element is TextBox) return element;
Visual result = null;
if (element is FrameworkElement)
(element as FrameworkElement).ApplyTemplate();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++)
{
Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
result = GetDescendantTextBox(visual);
if (result != null) break;
}
return result;
}