Another problem in my WPF series of questions :)
I am creating a custom Decorator that will be used to decorate the Panel (or it could be Behavior, no difference).
That Decorator deals with elements that are in this Panel ( Children property of the Panel ). It attaches some elements to these elements. Now I need the position of an element (the "child" Panel ) relative to the Panel itself. In other words, I need a position if there is any child in the Panel coordinate space. in other words, I need the offset that was determined by the ArrangeOverride Panel method when calling the Arrange method on Children .
It seems easy. But I can’t find a way to always get the correct coordinates.
This code
VisualTreeHelper.GetOffset(child)
it doesn’t work when the panel is inside ScrollView - it occupies the uppermost, most left visible corner of the Panel as a coordinate source - not the uppermost and left corner of the Panel .
Code
Point position = child.TransformToAncestor(panel).Transform(new Point(0,0));
will not work if some child transformations are already active in the Panel child. It will return the position of the transformed image (by transforming the visualization) of the child. Render position.
Same thing 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, I tried it, but it did not work. I have two similar questions on this topic that are related to simplify the problem, so I got some of the suggestions above. Now I have fully presented the problem, I hope to get the right advice.
If something is unclear, leave a comment. thank you