이러한 예제에서는 부모 또는 모든 상위 또는 하위 항목에 상대적인 시각적 개체의 오프셋 값을 검색하는 방법을 보여 줍니다.
예시
다음 태그 예제에서는 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 메서드를 사용합니다.
상위 항목에 상대적인 오프셋 가져오기
다음 태그 예제에서는 두 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>
다음 그림에서는 태그의 결과를 보여 줍니다.
개체의 오프셋 값VisualOffset_01
두 StackPanels 내에 중첩된 TextBlock
다음 코드 예제에서는 TransformToAncestor 메서드를 사용하여 포함하는 Window에 상대적인 TextBlock의 오프셋을 검색하는 방법을 보여 줍니다. 오프셋 값은 반환된 Vector 값 내에 포함됩니다.
// 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의 오프셋을 검색하는 방법을 보여 줍니다. 오프셋 값은 반환된 Vector 값 내에 포함됩니다.
// 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입니다. 부모 개체가 자식 개체에 상대적인 음수 오프셋이므로 오프셋 값은 음수 값입니다.
참고하십시오
.NET Desktop feedback