Visual.TransformToAncestor Method (Visual)
Assembly: PresentationCore (in PresentationCore.dll)
Parameters
- ancestor
- Type: System.Windows.Media.Visual
The Visual to which the coordinates are transformed.
| Exception | Condition |
|---|---|
| ArgumentNullException |
ancestor is null. |
| 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.
The following markup example shows a TextBlock that is nested within two StackPanel objects.
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.
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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.