WPF: find element location

Before answering, this is not as easy as you might think when you read the title.

I have an ItemControl that is bound to an ObservableCollection T and data that is described as a DataTemplate. So far, this is a classic case.

When I add a new item, I need to know the exact coordinate and position inside the box of the item being displayed.

I understand that collecting events takes the time it takes to collect the event and WPF uses its entire layout engine to actually position the element.

I want to be notified when this is done and grab those locations. I am using these 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 that when I hit these lines, it is always premature. If I "wait" for a second and give wPF, I get the right place.

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

Thanks! Ariel

0


a source to share


1 answer


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



0


a source







All Articles