-
August 7th, 2009, 05:56 AM
#1
Error in finding an element in a DataTemplate
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);
Keyboard.Focus(box);
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?
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|