Visual.TransformToAncestor Method

Definition

Returns a transform that can be used to transform coordinates from the Visual to the specified ancestor of the visual object.

Overloads

TransformToAncestor(Visual3D)

Returns a transform that can be used to transform coordinates from the Visual to the specified Visual3D ancestor of the visual object.

TransformToAncestor(Visual)

Returns a transform that can be used to transform coordinates from the Visual to the specified Visual ancestor of the visual object.

TransformToAncestor(Visual3D)

Returns a transform that can be used to transform coordinates from the Visual to the specified Visual3D ancestor of the visual object.

C#
public System.Windows.Media.Media3D.GeneralTransform2DTo3D TransformToAncestor(System.Windows.Media.Media3D.Visual3D ancestor);

Parameters

ancestor
Visual3D

The Visual3D to which the coordinates are transformed.

Returns

A transform that can be used to transform coordinates from the Visual to the specified Visual3D ancestor of the visual object.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

TransformToAncestor(Visual)

Returns a transform that can be used to transform coordinates from the Visual to the specified Visual ancestor of the visual object.

C#
public System.Windows.Media.GeneralTransform TransformToAncestor(System.Windows.Media.Visual ancestor);

Parameters

ancestor
Visual

The Visual to which the coordinates are transformed.

Returns

A value of type GeneralTransform.

Exceptions

ancestor is null.

ancestor is not an ancestor of the visual.

The visual objects are not related.

Examples

The following markup example shows a TextBlock that is nested within two StackPanel objects.

XAML
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <StackPanel Margin="16">
    <StackPanel Margin="8">
      <TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
    </StackPanel>
  </StackPanel>
</Window>

The following code example shows how to use the TransformToAncestor method to retrieve the offset of the TextBlock relative to the containing Window. The offset values are contained in the returned GeneralTransform.

C#
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myTextBlock.TransformToAncestor(this);

// Retrieve the point value relative to the parent.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

The offset takes into account the Margin values for all objects within the containing Window. In this case, X is 28, and Y is 28.

Remarks

Alternatively, you can use the VisualTreeHelper.GetOffset method to retrieve the offset of a visual object relative to its parent. The offset values are contained within the returned Vector value.

C#
// Return the offset vector for the TextBlock object.
Vector vector = VisualTreeHelper.GetOffset(myTextBlock);

// Convert the vector to a point value.
Point currentPoint = new Point(vector.X, vector.Y);

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10