This documentation is archived and is not being maintained.

TextPointer.GetNextContextPosition Method

Returns a pointer to the next symbol in the specified logical direction.

Namespace:  System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)

public TextPointer GetNextContextPosition(
	LogicalDirection direction


Type: System.Windows.Documents.LogicalDirection
One of the LogicalDirection values that specifies the logical direction in which to search for the next symbol.

Return Value

Type: System.Windows.Documents.TextPointer
A TextPointer to the next symbol in the requested direction, or null if the current TextPointer borders the start or end of content.

Any of the following is considered to be a symbol:

If the next symbol is categorized as EmbeddedElement, ElementStart, or ElementEnd (as identified by the GetPointerContext method), then the TextPointer returned by this method is advanced by exactly one symbol from the current position.

If the next symbol is categorized as Text, then the TextPointer returned by this method is advanced past the text to the next non-text symbol (that is, the next position where the TextPointerContext is not Text). The exact symbol count crossed can be calculated in advance by calling the GetTextRunLength method.

The following example demonstrates a use for this method. The example uses the GetNextContextPosition method in conjunction with the GetPointerContext method to traverse and extract the symbols in a specified TextElement.

While the example can be used to extract a XAML structure for the contents of a given TextElement, it is intended for illustrative purposes only, and should not be used in production code. See the System.Xml namespace for a rich set of types designed for working with and processing XML.

// This method will extract and return a string that contains a representation of 
// the XAML structure of content elements in a given TextElement.        
string GetXaml(TextElement element)
    StringBuilder buffer = new StringBuilder();

    // Position a "navigator" pointer before the opening tag of the element.
    TextPointer navigator = element.ElementStart;

    while (navigator.CompareTo(element.ElementEnd) < 0)
        switch (navigator.GetPointerContext(LogicalDirection.Forward))
            case TextPointerContext.ElementStart : 
                // Output opening tag of a TextElement
                buffer.AppendFormat("<{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
            case TextPointerContext.ElementEnd :
                // Output closing tag of a TextElement
                buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
            case TextPointerContext.EmbeddedElement :
                // Output simple tag for embedded element
                buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
            case TextPointerContext.Text :
                // Output the text content of this text run

        // Advance the naviagtor to the next context position.
        navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);

    } // End while.

    return buffer.ToString();

} // End GetXaml method.

.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.