语言

Visual.TransformToAncestor 方法

定义

返回一个转换,该转换可用于将坐标从 Visual 视觉对象的指定上级转换。

重载

名称 说明
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 不是视觉对象的上级。

视觉对象不相关。

示例

以下标记示例演示嵌套在两TextBlockStackPanel对象中。

<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该方法检索相对于包含TextBlockWindow偏移量。 偏移值包含在返回 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)

适用于