views:

82

answers:

1

Before answering, it is not as easy question as you might have thought about when you read the title.

I have an ItemsControl which is binded to an ObservableCollection of T and data being described as a DataTemplate. So far it is a classic case.

When I add a new element I need to know the exact coordinate and positions inside the window of the element being rendered.

I realize there is a passage of time needed for the Collection to raise the event, and WPF to use all his layouting mechanism to actually position the element.

I wish to be notified when it is done and grab those locations. I am using those lines of code

UIElement item = list.ItemContainerGenerator.ContainerFromItem(foo) as UIElement;
Point point = TranslatePoint(new Point(0.0, 0.0), Window.GetWindow(item));

The problem is now when I hit those lines it is always premature, If I "wait" for a second and let wPF finish, I do get the right location.

I am trying to find better solutions than "waiting" for the UI-Thread. Maybe you can help out :)

Thanks! Ariel

A: 

Probably one of UIElement's events will tell you when the layout data is available. LayoutUpdate looks promising.

Matthew Flaschen
Well, I tried that before. After your answer I re-checked my code. I needed to do a little more synchonization stuff but It did help out.Thanks.
ArielBH