Sometimes the following trick works for me:
// Replace such call:
SetFocusToNewItem(newIndex);
// By this:
Dispatcher.BeginInvoke((Action) (() =>
{
SetFocusToNewItem(newIndex);
}));
The problem is when you add an item in collection UI elements is not created, so you can't get it. This code above delays your function executing and you method should be executed after UI is created.
PS: You can move Dispatcher
call inside of your method.
EDIT:
This may help too (works for me):
var cp = theItemsControl.ItemContainerGenerator.ContainerFromIndex(index) as ContentPresenter;
cp.Loaded += (s, e2) =>
{
var tb = FindVisualChildren<TextBox>(e2.Source as UIElement)
.FirstOrDefault();
if (tb != null)
tb.Focus();
};
The reason is the same: UI element was not created.