Export (0) Print
Expand All

TextPointer.LogicalDirection Property

Gets the logical direction associated with the current position, which is used to disambiguate content associated with the current position.

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

public LogicalDirection LogicalDirection { get; }

Property Value

Type: System.Windows.Documents.LogicalDirection
The LogicalDirection value that is associated with the current position.

As an example of how this property is used, the LogicalDirection of the TextPointer returned by a hit test method gives a hit that is between two characters of text. The logical direction specifies which of the two characters was actually hit—the left or the right.

The following code uses the LogicalDirection property to underline the last word in the RichTextBox. In this example, a space character is used as the word boundary. This code example is part of a larger example used in the TextPointer class.

Run this sample


//This method underlines the last word in a RichTextBox
public void UnderlineLastWord()
{
    TextPointer EndofContent = MyRTB1.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
    TextPointer currentPointer = EndofContent.GetNextInsertionPosition(LogicalDirection.Backward);

    if (currentPointer == null)
        return;

    string currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);

    while (currentChar != " " && currentChar != "")
    {
        currentPointer = currentPointer.GetNextInsertionPosition(LogicalDirection.Backward);
        currentChar = GetCurrentChar(MyRTB1, currentPointer, LogicalDirection.Backward);
    }

    if (currentChar == " ")
        MyRTB1.Selection.Select(currentPointer.GetNextInsertionPosition(LogicalDirection.Forward), EndofContent);
    else
        MyRTB1.Selection.Select(currentPointer, EndofContent);

    MyRTB1.Selection.ApplyPropertyValue(Run.TextDecorationsProperty, TextDecorations.Underline);
}

private string GetCurrentChar(RichTextBox RTB, TextPointer pointer, LogicalDirection direction)
{
    TextPointer nextPointer = pointer.GetNextInsertionPosition(direction);
    if (nextPointer != null)
    {
        RTB.Selection.Select(pointer, nextPointer);
        if (RTB.Selection.Text.Length != 0)
            return RTB.Selection.Text[0].ToString();
    }
    return "";
}


Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft