TextPointer.GetPointerContext(LogicalDirection) Method

Definition

Returns a category indicator for the content adjacent to the current TextPointer in the specified logical direction.

C#
public System.Windows.Documents.TextPointerContext GetPointerContext(System.Windows.Documents.LogicalDirection direction);

Parameters

direction
LogicalDirection

One of the LogicalDirection values that specifies the logical direction in which to determine the category for adjacent content.

Returns

One of the TextPointerContext values that indicates the category for adjacent content in the specified logical direction.

Examples

The following example demonstrates a use for this method. The example uses the GetPointerContext method to implement an algorithm for calculating the balance of opening and closing element tags between two specified TextPointer positions. Each opening element tag is counted as +1, and each closing element tag is counted as -1.

C#
// Calculate and return the relative balance of opening and closing element tags
// between two specified TextPointers.
int GetElementTagBalance(TextPointer start, TextPointer end)
{
    int balance = 0;
 
    while (start != null && start.CompareTo(end) < 0)
    {
        TextPointerContext forwardContext = start.GetPointerContext(LogicalDirection.Forward);
 
        if (forwardContext == TextPointerContext.ElementStart)     balance++;
        else if (forwardContext == TextPointerContext.ElementEnd)  balance--;
             
        start = start.GetNextContextPosition(LogicalDirection.Forward);
    } // End while.
 
    return balance;
} // End GetElementTagBalance

Applies to

Prodotto Versioni
.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