Visual.TransformToAncestor

Visual has a TransformToAncestor method that returns a GeneralTransform2DTo3D. This is useful when a Visual is hosted by a Viewport2DVisual3D. The returned object converts the hosted Visual’s 2D coordinate space into the 3D coordinate space of the Visual3D.

In Listings 8.8 and 8.9, the Point (0,0) from the Viewport2DVisual3D’s hosted Button is mapped into 3D space, and a purple cube is drawn where that Point3D lies in 3D space. As the larger cube rotates, the smaller cube follows it because the GeneralTransform2DTo3D changes as the larger cube rotates. Figure 8.52 shows the result.

LISTING 8.8 MainWindow.xaml: The Cube of Buttons and the Small Purple Cube

<Window x:Class="MainWindow"

Get XAML Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.