WPF: Viewbox and TranslatePoint
- I have a Viewbox with a Canvas Child,
- I have the Stretch property of Viewbox to Fill,
- I have changed the width and height o开发者_Python百科f the Viewbox,
I need to get the location of children in Canvas with respect to Viewbox parent I tried :
Point p = viewboxInstance.TranslatePoint(new Point(Canvas.GetLeft(child), Canvas.GetTop(child)), viewboxInstanceParent);
it gets wrong coordinates! Is there a solution or work around ?
Thanks
The ViewBox
does not change any properties of its children including Canvas
left and top values. You need to use the actual coordinates of the child, not just the attached property values.
You can call TranslatePoint
directly on the child as long as it's a UIElement
. To reference the top-left point of the child, you can use (0, 0) which is the default for Point
so new Point()
is enough.
Point p = child.TranslatePoint(new Point(), viewboxInstanceParent);
good luck,
EDIT: Here's a video of this code in action: ViewboxChildPosition
精彩评论