Information
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

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

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

public GeneralTransform TransformToAncestor (
	Visual ancestor
)
public GeneralTransform TransformToAncestor (
	Visual ancestor
)
public function TransformToAncestor (
	ancestor : Visual
) : GeneralTransform
You cannot use methods in XAML.

Parameters

ancestor

The Visual to which the coordinates are transformed.

Return Value

A value of type GeneralTransform.

Exception typeCondition

ArgumentNullException

ancestor is a null reference (Nothing in Visual Basic).

ArgumentException

ancestor is not an ancestor of the visual.

InvalidOperationException

The visual objects are not related.

Alternatively, you can use the 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="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 within the returned GeneralTransform value.

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft