Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TextPointer.GetNextContextPosition Method (LogicalDirection)


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
Available since 3.0
Return to top
© 2017 Microsoft