TextPointer.GetNextContextPosition(LogicalDirection) Method

Definition

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

C#
public System.Windows.Documents.TextPointer GetNextContextPosition(System.Windows.Documents.LogicalDirection direction);

Parameters

direction
LogicalDirection

One of the LogicalDirection values that specifies the logical direction in which to search for the next symbol.

Returns

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

Examples

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.

C#
// 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);
                break;
            case TextPointerContext.ElementEnd :
                // Output closing tag of a TextElement
                buffer.AppendFormat("</{0}>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                break;
            case TextPointerContext.EmbeddedElement :
                // Output simple tag for embedded element
                buffer.AppendFormat("<{0}/>", navigator.GetAdjacentElement(LogicalDirection.Forward).GetType().Name);
                break;
            case TextPointerContext.Text :
                // Output the text content of this text run
                buffer.Append(navigator.GetTextInRun(LogicalDirection.Forward));
                break;
        }
 
        // Advance the naviagtor to the next context position.
        navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
    } // End while.

    return buffer.ToString();
} // End GetXaml method.

Remarks

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.

Applies to

Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10