VisualTreeHelper.GetOffset(Visual) Method

Definition

Returns the offset of the Visual.

C#
public static System.Windows.Vector GetOffset(System.Windows.Media.Visual reference);

Parameters

reference
Visual

The Visual whose offset is returned.

Returns

A Vector that represents the offset value of the Visual.

Examples

The following markup example shows a TextBlock that is defined with Margin value of 4.

XAML
<TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />

The following code example shows how to use the GetOffset method to retrieve the offset of the TextBlock. The offset values are contained within the returned Vector value.

C#
// 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 offset takes into account the Margin value. In this case, X is 4, and Y is 4.

Remarks

The returned offset value is relative to the parent of the Visual. If you want to return an offset value that is not relative to the parent of a Visual, use the TransformToAncestor method.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10