Share via


VisualTreeHelper.GetTransform(Visual) 方法

定義

傳回 TransformVisual 值。

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

傳回

Visual 的轉換值,如果 reference 沒有定義轉換則為 null

範例

下列程式碼範例示範如何使用 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))

適用於