5

Another problem in my WPF questions series :)

I'm creating custom Decorator that will be used to decorate Panels (or it can be Behavior, no difference).

That Decorator deals with elements that are in that Panel (Children property of the Panel). It attaches some RenderTransforms to those elements. Now I need a position of some element ('child' of the Panel) relative to the Panel itself. In other words I need a position if some child element in the Panels coordinate space. in just another words I want the offset that was specified by the ArrangeOverride method of the Panel when calling Arrange method on Children.

That seem to be easy. But I cannot find the way to always get right coordinates.

This code

VisualTreeHelper.GetOffset(child)

does not work when the panel is inside ScrollView - it takes topmost, leftmost visible corner of the Panel as an origin of coordinate space - not the real topmost and leftmost corner of the Panel.

The code

Point position = child.TransformToAncestor(panel).Transform(new Point(0,0));

will not work when some render transforms are already active on the child element of the Panel. It will return the position of transformed image(by the render transfrom) of child element. The render position.

The same problem is with this aproach:

Point panelPosition = panel.PointToScreen(new Point(0, 0));
Point childPosition = child.PointToScreen(new Point(0, 0));
Point position = new Point(childPosition.X - panelPosition.X, childsPosition.Y - panelPosition.Y);

So this i what I have tryied but it did not work. I have 2 similar questions on this topic that were tying to simplify the problem, so I got some of suggestions above. Now I introduced the problem in its full complexity, I hope to get the right advice.

If something is unclear please leave the comment. Thank you

Rasto
  • 17,204
  • 47
  • 154
  • 245
  • 1
    If you use Canvas panel you can call Canvas.GetTop and Canvas.GetLeft to determine the top and left coordinates on the 2D plane. – Bhupendra Jan 22 '11 at 18:10
  • Thank you. But I don't know what type my `Panel` is. It is given to me, the `Decorator` should be able to decorate any `Panel`. – Rasto Jan 22 '11 at 18:23
  • OK, I am not sure as of now but check this if it helps http://stackoverflow.com/questions/4760310/wpf-get-position-of-child-uielement-within-its-parent-ignore-rendertransform-if – Bhupendra Jan 22 '11 at 19:08
  • @bjoshi: That is one of my questions about this topic that I mentioned in this question. Sadly, it does not help :( – Rasto Jan 22 '11 at 19:41
  • 1
    Can this be solved as described in http://stackoverflow.com/questions/1923697/how-can-i-get-the-position-of-a-child-element-relative-to-a-parent ? – Richard J Foster May 09 '13 at 21:11

1 Answers1

0

You can "undo" the RenderTransform before calling TransformToAncestor:

Point origin = child.RenderTransform.Inverse.Transform(new Point(0, 0));
Point position = child.TransformToAncestor(listBox).Transform(origin); 
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
  • Sadly this also does not work. `TransformToAncestor` takes as a beginig of coordinate space left top **visible** corner... – Rasto Jan 23 '11 at 09:14