Skip to main content
.NET Framework Class Library
VisualTransformToAncestor 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)
Syntax
Public Function TransformToAncestor ( _
	ancestor As [%$TOPIC/ms608865_en-us_VS_110_3_0_0_0_0%] _
) As [%$TOPIC/ms608865_en-us_VS_110_3_0_0_0_1%]
public [%$TOPIC/ms608865_en-us_VS_110_3_0_1_0_0%] TransformToAncestor(
	[%$TOPIC/ms608865_en-us_VS_110_3_0_1_0_1%] ancestor
)
public:
[%$TOPIC/ms608865_en-us_VS_110_3_0_2_0_0%]^ TransformToAncestor(
	[%$TOPIC/ms608865_en-us_VS_110_3_0_2_0_1%]^ ancestor
)
member TransformToAncestor : 
        ancestor:[%$TOPIC/ms608865_en-us_VS_110_3_0_3_0_0%] -> [%$TOPIC/ms608865_en-us_VS_110_3_0_3_0_1%]

Parameters

ancestor
Type: System.Windows.MediaVisual

The Visual to which the coordinates are transformed.

Exceptions
ExceptionCondition
ArgumentNullException

ancestor is .

ArgumentException

ancestor is not an ancestor of the visual.

InvalidOperationException

The visual objects are not related.

Remarks

Alternatively, you can use the VisualTreeHelperGetOffset 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.
			Dim vector As Vector = VisualTreeHelper.GetOffset(myTextBlock)

			' Convert the vector to a point value.
			Dim currentPoint As New Point(vector.X, vector.Y)
// 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);
Examples

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

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

			' Return the general transform for the specified visual object.
			Dim generalTransform1 As GeneralTransform = myTextBlock.TransformToAncestor(Me)

			' Retrieve the point value relative to the parent.
			Dim currentPoint As Point = generalTransform1.Transform(New Point(0, 0))
// 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.

Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.