Visual.TransformToAncestor 方法

定义

返回一个转换,该转换可用于将 Visual 中的坐标转换为指定的可视对象上级。Returns a transform that can be used to transform coordinates from the Visual to the specified ancestor of the visual object.

重载

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)

返回一个转换,该转换可用于将 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)

返回一个转换,该转换可用于将 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

要将坐标转换到的 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)

返回一个转换,该转换可用于将 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

要将坐标转换到的 VisualThe Visual to which the coordinates are transformed.

返回

一个 GeneralTransform 类型的值。A value of type GeneralTransform.

异常

ancestornullancestor is null.

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相对于所包含Window的的偏移量。The following code example shows how to use the TransformToAncestor method to retrieve the offset of the TextBlock relative to the containing Window. 偏移量值包含在返回GeneralTransform的中。The 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将考虑包含Window中的所有对象的值。The 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)

适用于