次の方法で共有


方法: ビジュアルのオフセットを取得する

これらの例では、ビジュアル オブジェクトの親、または先祖や子孫に対する相対的なオフセット値を取得する方法を示します。

次のマークアップの例は、Margin 値 4 で定義されている TextBlock を示しています。

<TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />

次のコード例は、GetOffset メソッドを使用して、TextBlock のオフセットを取得する方法を示しています。 オフセット値は、返された 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)

オフセットでは、Margin 値が考慮されます。 この場合、X は 4 で、Y は 4 です。

返されるオフセット値は、Visual の親に対する相対値です。 Visual の親に対して相対的ではないオフセット値を返す場合は、TransformToAncestor メソッドを使用します。

先祖に対する相対的なオフセットの取得

次のマークアップの例は、2 つの StackPanel オブジェクト内で入れ子になっている TextBlock を示しています。

<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>

次の図は、マークアップの結果を示しています。

Offset values of objects
2 つの StackPanels 内で入れ子になった TextBlock

次のコード例は、TransformToAncestor メソッドを使用して、含まれている Window に対する TextBlock 相対的なオフセットを取得する方法を示しています。 オフセット値は、返された 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))

オフセットでは、オブジェクトが含まれる Window 内のすべてのオブジェクトの Margin 値が考慮されます。 この場合、X は 28 (16 + 8 + 4) で、Y は 28 です。

返されるオフセット値は、Visual の先祖に対する相対値です。 Visual の子孫に対して相対的なオフセット値を返す場合は、TransformToDescendant メソッドを使用します。

子孫に対する相対的なオフセットの取得

次のマークアップの例は、StackPanel オブジェクト内に含まれる TextBlock を示しています。

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

次のコード例は、TransformToDescendant メソッドを使用して、その子 TextBlock に対する StackPanel の相対的なオフセットを取得する方法を示します。 オフセット値は、返された GeneralTransform 値内に含まれます。

// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToDescendant(myTextBlock);

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));
' Return the general transform for the specified visual object.
Dim generalTransform1 As GeneralTransform = myStackPanel.TransformToDescendant(myTextBlock)

' Retrieve the point value relative to the child.
Dim currentPoint As Point = generalTransform1.Transform(New Point(0, 0))

オフセットでは、すべてのオブジェクトの Margin 値が考慮されます。 この場合、X は -4 で、Y は -4 です。 親オブジェクトはその子オブジェクトに対する相対的な負のオフセットであるため、オフセット値は負の値になります。

関連項目