Visual.TransformToDescendant(Visual) Method

Definition

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

C#
public System.Windows.Media.GeneralTransform TransformToDescendant(System.Windows.Media.Visual descendant);

Parameters

descendant
Visual

The Visual to which the coordinates are transformed.

Returns

A value of type GeneralTransform.

Exceptions

descendant is null.

The visual is not an ancestor of the descendant visual.

The visual objects are not related.

Examples

The following markup example shows a TextBlock that is contained within a StackPanel object.

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

The following code example shows how to use the TransformToDescendant method to retrieve the offset of the StackPanel relative to its child TextBlock. The offset values are contained within the returned GeneralTransform value.

C#
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToDescendant(myTextBlock);

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

The offset takes into account the Margin values for all objects. In this case, X is -4, and Y is -4. The offset values are negative values because the parent object is negatively offset relative to its child object.

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