语言

VisualTreeHelper.GetTransform(Visual) 方法

定义

返回一个Transform值。Visual

public:
 static System::Windows::Media::Transform ^ GetTransform(System::Windows::Media::Visual ^ reference);
public static System.Windows.Media.Transform GetTransform(System.Windows.Media.Visual reference);
static member GetTransform : System.Windows.Media.Visual -> System.Windows.Media.Transform
Public Shared Function GetTransform (reference As Visual) As Transform

参数

reference
Visual

Visual返回其转换值。

返回

或Visualnull未定义转换的转换值的转换值reference。

示例

下面的代码示例演示如何使用 GetTransform 该方法检索指定视觉对象的转换。

// Return the transform for the specified visual object.
Transform transform = VisualTreeHelper.GetTransform(myDrawing);

// If there is no transform defined for the object, the return value is null.
if (transform != null)
{
    // Return the offset of the returned transform. The offset is relative to the parent of the visual object.
    Point pt = transform.Transform(new Point(0, 0));
}
' Return the transform for the specified visual object.
Dim transform As Transform = VisualTreeHelper.GetTransform(myDrawing)

' If there is no transform defined for the object, the return value is null.
If transform IsNot Nothing Then
    ' Return the offset of the returned transform. The offset is relative to the parent of the visual object.
    Dim pt As Point = transform.Transform(New Point(0, 0))
End If

代码应在使用返回的值之前进行测试 null 。 若要返回返回的转换的偏移量,请使用 Transform 该方法。 偏移值相对于 . 的 Visual父级 。

注解

或者,可以使用 TransformToAncestor 该方法为上级 Visual返回转换值。 此方法始终返回有效的转换。 下面的代码示例演示如何使用 TransformToAncestor 方法。

// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myTextBlock.TransformToAncestor((Visual)myTextBlock.Parent);

// Retrieve the point value relative to the parent.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));
' Return the general transform for the specified visual object.
Dim generalTransform1 As GeneralTransform = myTextBlock.TransformToAncestor(CType(myTextBlock.Parent, Visual))

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

适用于