tags:

views:

146

answers:

2

I have a stackpanel that has N-number of children. I want to get the x,y coordinates of the children relative to the parent stackpanel.

The children in the stackpanel are centered horizontally so whenever the stackpanel resizes the children are centered. Here's what I'm doing to get the TopLeft corner of the child item relative to the StackPanel:

Dim parent = VisualTreeHelper.GetParent(childItem)
childItem.TranslatePoint(VisualTreeHelper.GetDescendantBounds(childItem).TopLeft, parent)

Let's say in the initial layout the point it gives me back is (20,0). Now the layout changes and the stackpanel widens, but the children remain centered. I would expect this method now to give me something back like (150, 0) but it's still giving me (20,0). Visually I can see the items are in the center of the StackPanel, but it's still giving me the initial coordinates. What am I doing wrong?

A: 

Just guessing because I don't know what FindAncestors.FindAncestor does, but I'm guessing it returns the container of the item rather than horizontally-centered content of the container. You want the top-left coordinate of the content, not the container.

HTH, Kent

Kent Boogaart
I changed the code to make it more undstandable (I hope). I'm getting the parent container and then translating the bounds of the child to it's parent
Micah
+1  A: 

I think you need to pass new Point(0,0) to the TranslatePoint function. Since the child is translating a point in its coordinate space to another visual, its upper left corner is (0,0).

Abe Heidebrecht