The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Visual.TransformToAncestor Method (Visual)


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

Namespace:   System.Windows.Media
Assembly:  PresentationCore (in PresentationCore.dll)

public GeneralTransform TransformToAncestor(
	Visual ancestor


Type: System.Windows.Media.Visual

The Visual to which the coordinates are transformed.

Return Value

Type: System.Windows.Media.GeneralTransform

A value of type GeneralTransform.

Exception Condition

ancestor is null.


ancestor is not an ancestor of the visual.


The visual objects are not related.

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.

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

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

<Window xmlns=""
        xmlns:x="" >
  <StackPanel Margin="16">
    <StackPanel Margin="8">
      <TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />

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.

// 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.

.NET Framework
Available since 3.0
Return to top
© 2015 Microsoft