WPFwiki, wpf, wiki, .net 3.0, windows presentation foundation, FAQ, free resources, solution, development, microsoft Home of the world's largest WPF FAQ
Edit

Category

Layout

Edit

Question

Is is possible to convert a point or rectangle from a particular Visual's coordinate system to another?

Edit

Answer

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);


All content is Copyright ©2007 Xceed Software Inc. unless otherwise indicated. See the Terms of Service. Contributors must read and agree to the Contribution Policy. WPFwiki is brought to you by Xceed, makers of the powerful yet free Xceed DataGrid for WPF.