Visual.TransformToAncestor Visual.TransformToAncestor Visual.TransformToAncestor Visual.TransformToAncestor Method

定義

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定祖系。Returns a transform that can be used to transform coordinates from the Visual to the specified ancestor of the visual object.

多載

TransformToAncestor(Visual3D) TransformToAncestor(Visual3D) TransformToAncestor(Visual3D) TransformToAncestor(Visual3D)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual3D 祖系。Returns a transform that can be used to transform coordinates from the Visual to the specified Visual3D ancestor of the visual object.

TransformToAncestor(Visual) TransformToAncestor(Visual) TransformToAncestor(Visual) TransformToAncestor(Visual)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual 祖系。Returns a transform that can be used to transform coordinates from the Visual to the specified Visual ancestor of the visual object.

TransformToAncestor(Visual3D) TransformToAncestor(Visual3D) TransformToAncestor(Visual3D) TransformToAncestor(Visual3D)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual3D 祖系。Returns a transform that can be used to transform coordinates from the Visual to the specified Visual3D ancestor of the visual object.

public:
 System::Windows::Media::Media3D::GeneralTransform2DTo3D ^ TransformToAncestor(System::Windows::Media::Media3D::Visual3D ^ ancestor);
public System.Windows.Media.Media3D.GeneralTransform2DTo3D TransformToAncestor (System.Windows.Media.Media3D.Visual3D ancestor);
member this.TransformToAncestor : System.Windows.Media.Media3D.Visual3D -> System.Windows.Media.Media3D.GeneralTransform2DTo3D
Public Function TransformToAncestor (ancestor As Visual3D) As GeneralTransform2DTo3D

參數

ancestor
Visual3D Visual3D Visual3D Visual3D

座標會轉換為 Visual3DThe Visual3D to which the coordinates are transformed.

傳回

轉換,可用來將座標從 Visual 轉換為視覺物件之指定的 Visual3D 上階。A transform that can be used to transform coordinates from the Visual to the specified Visual3D ancestor of the visual object.

TransformToAncestor(Visual) TransformToAncestor(Visual) TransformToAncestor(Visual) TransformToAncestor(Visual)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual 祖系。Returns a transform that can be used to transform coordinates from the Visual to the specified Visual ancestor of the visual object.

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

參數

ancestor
Visual Visual Visual Visual

座標會轉換為 VisualThe Visual to which the coordinates are transformed.

傳回

例外狀況

ancestor 不是視覺物件的祖系。ancestor is not an ancestor of the visual.

這些視覺物件都不會產生關聯。The visual objects are not related.

範例

下列標記範例示範TextBlock巢狀內兩個StackPanel物件。The following markup example shows a TextBlock that is nested within two StackPanel objects.

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
  <StackPanel Margin="16">
    <StackPanel Margin="8">
      <TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
    </StackPanel>
  </StackPanel>
</Window>

下列程式碼範例示範如何使用TransformToAncestor方法來擷取的位移TextBlock相對於包含WindowThe following code example shows how to use the TransformToAncestor method to retrieve the offset of the TextBlock relative to the containing Window. 位移的值都包含在傳回GeneralTransformThe offset values are contained in the returned GeneralTransform.

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

// 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(Me)

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

位移會考量Margin內包含的所有物件的值WindowThe offset takes into account the Margin values for all objects within the containing Window. 在此情況下,X為 28,和Y為 28。In this case, X is 28, and Y is 28.

備註

或者,您可以使用VisualTreeHelper.GetOffset方法來擷取相對於其父視覺物件的位移。Alternatively, you can use the VisualTreeHelper.GetOffset method to retrieve the offset of a visual object relative to its parent. 位移的值都包含在傳回Vector值。The offset values are contained within the returned Vector value.

// 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);
' Return the offset vector for the TextBlock object.
Dim vector As Vector = VisualTreeHelper.GetOffset(myTextBlock)

' Convert the vector to a point value.
Dim currentPoint As New Point(vector.X, vector.Y)

適用於