Visual.TransformToVisual(Visual) Method

Definition

返回一个转换,该转换可用于将 Visual 中的坐标转换为指定的可视对象。Returns a transform that can be used to transform coordinates from the Visual to the specified visual object.

public:
 System::Windows::Media::GeneralTransform ^ TransformToVisual(System::Windows::Media::Visual ^ visual);
public System.Windows.Media.GeneralTransform TransformToVisual (System.Windows.Media.Visual visual);
member this.TransformToVisual : System.Windows.Media.Visual -> System.Windows.Media.GeneralTransform
Public Function TransformToVisual (visual As Visual) As GeneralTransform

Parameters

visual
Visual

要将坐标转换到的 VisualThe Visual to which the coordinates are transformed.

Returns

GeneralTransform

一个 GeneralTransform 类型的值。A value of type GeneralTransform.

Exceptions

visualnullvisual is null.

这些可视对象不相关。The visual objects are not related.

Examples

以下标记示例显示了包含在 StackPanel 对象中的 TextBlockThe following markup example shows a TextBlock that is contained within a StackPanel object.

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

下面的代码示例演示如何使用 TransformToVisual 方法检索相对于其子 TextBlockStackPanel 偏移量。The following code example shows how to use the TransformToVisual method to retrieve the offset of the StackPanel relative to its child TextBlock. 偏移量值包含在返回的 GeneralTransform 值中。The offset values are contained within the returned GeneralTransform value.

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

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));
' Return the general transform for the specified visual object.
Dim generalTransform1 As GeneralTransform = myStackPanel.TransformToVisual(myTextBlock)

' Retrieve the point value relative to the child.
Dim currentPoint As Point = generalTransform1.Transform(New Point(0, 0))

偏移量将考虑所有对象的 Margin 值。The offset takes into account the Margin values for all objects. 在这种情况下,X 是-4,Y 为-4。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.

Remarks

TransformToAncestorTransformToDescendant 方法还可用于返回视觉对象的转换。The TransformToAncestor and TransformToDescendant methods can also be used to return a transform for a visual object.

Applies to