Visual.TransformToAncestor 方法

定義

回傳一個轉換,可用於將座標從 Visual 轉換到視覺物件指定的祖先。

多載

名稱 Description
TransformToAncestor(Visual3D)

回傳一個轉換,可用於將座標從 Visual 轉換到視覺物件指定的 Visual3D 祖先。

TransformToAncestor(Visual)

回傳一個轉換,可用於將座標從 Visual 轉換到視覺物件指定的 Visual 祖先。

TransformToAncestor(Visual3D)

回傳一個轉換,可用於將座標從 Visual 轉換到視覺物件指定的 Visual3D 祖先。

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 會被轉換到。

傳回

一種可以用來將座標從 Visual 轉換到視覺物件指定的 Visual3D 祖先的轉換。

適用於

TransformToAncestor(Visual)

回傳一個轉換,可用於將座標從 Visual 轉換到視覺物件指定的 Visual 祖先。

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 會被轉換到。

傳回

型別 GeneralTransform 的值。

例外狀況

ancestornull

ancestor 並非視覺的祖先。

視覺上的物件彼此無關。

範例

以下標記範例顯示 一個 TextBlock 巢狀於兩個 StackPanel 物件內。

<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的 的偏移量Window。 偏移值包含在回傳 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的所有物件的值。Window 在此情況下, X 是28,且 Y 是28。

備註

或者,你也可以用這個 VisualTreeHelper.GetOffset 方法來取得視覺物件相對於父物件的偏移量。 偏移值包含在回傳 Vector 值中。

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

適用於