Do you use your data template as an element template for an element container control, for example. Listbox Take a look at the KeyboardNavigation class , you can set its TabNavigation property to Continue or Loop for the container of your elements, smth for example:
<ListBox x:Name="myListBox"
KeyboardNavigation.TabNavigation="Continue"
ItemTemplate="{StaticResource myDataTemplate}"
...
when the focus is changed using the tab key in the ListBox, the focus will move from each item, and when the last item is reached, the focus will return to the first item "Loop" or move to the next custom control in the form if "Continue".
hope this helps, believes
edit0:
<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;
}