返回一個轉換,該轉換可用於將座標從Visual轉換爲指定的視覺對象。
public System.Windows.Media.GeneralTransform TransformToVisual (System.Windows.Media.Visual visual);
一個例子
以下標記示例顯示了StackPanel對象中包含的TextBlock。
<StackPanel Name="myStackPanel" Margin="8">
<TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
</StackPanel>
下面的代碼示例演示如何使用TransformToVisual方法獲取其子TextBlock的StackPanel的偏移量。偏移值包含在返回的GeneralTransform值中。
// 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方法,還可以返回可視對象的轉換。