Visual.TransformToAncestor 方法

定義

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定祖系。

多載

TransformToAncestor(Visual3D)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual3D 祖系。

TransformToAncestor(Visual)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual 祖系。

TransformToAncestor(Visual3D)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual3D 祖系。

C#
public System.Windows.Media.Media3D.GeneralTransform2DTo3D TransformToAncestor (System.Windows.Media.Media3D.Visual3D ancestor);

參數

ancestor
Visual3D

座標會轉換為 Visual3D

傳回

GeneralTransform2DTo3D

轉換,可用來將座標從 Visual 轉換為視覺物件之指定的 Visual3D 上階。

適用於

.NET Framework 4.8 和其他版本
產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7

TransformToAncestor(Visual)

傳回轉換,這個轉換可用來將 Visual 的座標轉換為視覺物件的指定 Visual 祖系。

C#
public System.Windows.Media.GeneralTransform TransformToAncestor (System.Windows.Media.Visual ancestor);

參數

ancestor
Visual

座標會轉換為 Visual

傳回

GeneralTransform

型別 GeneralTransform 的值。

例外狀況

ancestornull

ancestor 不是視覺物件的祖系。

這些視覺物件都不會產生關聯。

範例

下列標記範例顯示巢狀于兩 StackPanelTextBlock 物件內的 。

XAML
<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 方法來擷取相對於包含 Window 之 的位 TextBlock 移。 位移值包含在傳 GeneralTransform 回的 中。

C#
// 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));

位移會將包含 Window 內的所有物件值納入考慮 Margin 。 在此情況下, X 為 28,且 Y 為 28。

備註

或者,您可以使用 VisualTreeHelper.GetOffset 方法來擷取相對於其父物件的位移。 位移值包含在傳 Vector 回的值內。

C#
// 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);

適用於

.NET Framework 4.8 和其他版本
產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
Windows Desktop 3.0, 3.1, 5, 6, 7