EditCategory
Layout
EditQuestion
Is is possible to convert a point or rectangle from a particular Visual's coordinate system to another?
EditAnswer
The Visual class defines three methods, called:
- TransformToAncestor()
- TransformToDescendant()
- TransformToVisual()
These three methods return a GeneralTransform object, which can then be used to convert a point or rectangle to the desired Visual's coordinate system.
If I want to convert a point from an "object"'s coordinate system to its ancestor's coordinate system, the syntax is as follows:
GeneralTransform myTransform = myObject.TransformToAncestor( ancestor);
Point myPoint = myTransform.Transform( originalPoint);
Where ancestor is the desired Visual ancestor.
If I want to convert a point from an "object"'s coordinate system to its child coordinate system, the syntax is as follows:
GeneralTransform myTransform = myObject.TransformToAncestor( child);
Point myPoint = myTransform.Transform( originalPoint);
Where child is the desired Visual child (descendant).
Given the more complex case where I want to convert from any given visual to another visual's coordinate system, the syntax is then the following:
GeneralTransform myTransform = myObject.TransformToVisual( otherVisual);
Point myPoint = myTransform.Transform( originalPoint);