I am having an ItemsControl with a DataTemplate to show a collection of object. I add object to collection at run time. After adding an object, I want to set focus to a textbox in the recently added object's template. I do this by trying to find out the textbox immediately after adding the object with the code below.

ContentPresenter cp = OrderLinesList.ItemContainerGenerator.ContainerFromItem(line) as ContentPresenter;
DataTemplate dt = cp.ContentTemplate;
TextBox box = (TextBox)dt.FindName("CommonQuantTextBox", cp);

I am getting the following error in the FindName in the third line.
{"This operation is valid only on elements that have this template applied."}

The status of ItemContainerGenerator is ContainersGenerated.
This works if I have the same code somewhere else like for example on editing some control after the object is added or somethign like that.
So I am just wondering if there is some problem in immediately finding the element, somthing like the control is not loaded or soemthing?