Another problem in my WPF questions series :)
I'm creating custom Decorator
that will be used to decorate Panel
s (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 RenderTransform
s 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 Panel
s 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