--- title: "How to: Get the Offset of a Visual" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "getting offset values from visual objects [WPF]" - "offset values [WPF], retrieving from visual objects [WPF]" - "visual objects [WPF], retrieving offset values from" - "retrieving offset values from visual objects [WPF]" ms.assetid: 889a1dd6-1b11-445a-b351-fbb04c53ee34 --- # How to: Get the Offset of a Visual These examples show how to retrieve the offset value of a visual object that is relative to its parent, or any ancestor or descendant. ## Example The following markup example shows a that is defined with value of 4. [!code-xaml[VisualSnippets#VisualSnippet1](~/samples/snippets/csharp/VS_Snippets_Wpf/VisualSnippets/CSharp/Window1.xaml#visualsnippet1)] The following code example shows how to use the method to retrieve the offset of the . The offset values are contained within the returned value. [!code-csharp[VisualSnippets#VisualSnippet2](~/samples/snippets/csharp/VS_Snippets_Wpf/VisualSnippets/CSharp/Window1.xaml.cs#visualsnippet2)] [!code-vb[VisualSnippets#VisualSnippet2](~/samples/snippets/visualbasic/VS_Snippets_Wpf/VisualSnippets/visualbasic/window1.xaml.vb#visualsnippet2)] The offset takes into account the value. In this case, is 4, and is 4. The returned offset value is relative to the parent of the . If you want to return an offset value that is not relative to the parent of a , use the method. ## Getting the Offset Relative to an Ancestor The following markup example shows a that is nested within two objects. [!code-xaml[VisualSnippets#VisualSnippet7](~/samples/snippets/csharp/VS_Snippets_Wpf/VisualSnippets/CSharp/Window2.xaml#visualsnippet7)] The following illustration shows the results of the markup. ![Offset values of objects](./media/visualoffset-01.png "VisualOffset_01") TextBlock nested within two StackPanels The following code example shows how to use the method to retrieve the offset of the relative to the containing . The offset values are contained within the returned value. [!code-csharp[VisualSnippets#VisualSnippet5](~/samples/snippets/csharp/VS_Snippets_Wpf/VisualSnippets/CSharp/Window1.xaml.cs#visualsnippet5)] [!code-vb[VisualSnippets#VisualSnippet5](~/samples/snippets/visualbasic/VS_Snippets_Wpf/VisualSnippets/visualbasic/window1.xaml.vb#visualsnippet5)] The offset takes into account the values for all objects within the containing . In this case, is 28 (16 + 8 + 4), and is 28. The returned offset value is relative to the ancestor of the . If you want to return an offset value that is relative to the descendant of a , use the method. ## Getting the Offset Relative to a Descendant The following markup example shows a that is contained within a object. [!code-xaml[VisualSnippets#VisualSnippet4](~/samples/snippets/csharp/VS_Snippets_Wpf/VisualSnippets/CSharp/Window1.xaml#visualsnippet4)] The following code example shows how to use the method to retrieve the offset of the relative to its child . The offset values are contained within the returned value. [!code-csharp[VisualSnippets#VisualSnippet9](~/samples/snippets/csharp/VS_Snippets_Wpf/VisualSnippets/CSharp/Window1.xaml.cs#visualsnippet9)] [!code-vb[VisualSnippets#VisualSnippet9](~/samples/snippets/visualbasic/VS_Snippets_Wpf/VisualSnippets/visualbasic/window1.xaml.vb#visualsnippet9)] The offset takes into account the values for all objects. In this case, is -4, and is -4. The offset values are negative values, since the parent object is negatively offset relative to its child object. ## See also - - - [WPF Graphics Rendering Overview](wpf-graphics-rendering-overview.md)