Visual TransformToVisual(Visual)方法

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

public System.Windows.Media.GeneralTransform TransformToVisual (System.Windows.Media.Visual visual);

一个例子

以下标记示例显示了StackPanel对象中包含的TextBlock

XAML复制
 
<StackPanel Name="myStackPanel" Margin="8">
  <TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
</StackPanel>

下面的代码示例演示如何使用TransformToVisual方法获取其子TextBlockStackPanel的偏移量。偏移值包含在返回的GeneralTransform值中。

C#复制
 
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToVisual(myTextBlock);

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

偏移量考虑所有对象的边距值。在这种情况下,X为-4,Y为-4。偏移值为负,因为父对象相对于其子对象具有负偏移。

注解

使用TransformToAncestor方法和使用TransformToDescendant方法,还可以返回可视对象的转换。

猜你喜欢

转载自blog.csdn.net/qq_28368039/article/details/106569402