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)

member TransformToAncestor : 
        ancestor:Visual -> GeneralTransform 

Parameters

ancestor
Type: System.Windows.Media.Visual
The Visual to which the coordinates are transformed.

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

No code example is currently available or this language may not be supported.

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.

No code example is currently available or this language may not be supported.

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

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2015 Microsoft