TextPointer 类

定义

表示 FlowDocumentTextBlock 中的一个位置。Represents a position within a FlowDocument or TextBlock.

public ref class TextPointer : System::Windows::Documents::ContentPosition
public class TextPointer : System.Windows.Documents.ContentPosition
type TextPointer = class
    inherit ContentPosition
Public Class TextPointer
Inherits ContentPosition
继承
TextPointer

示例

下面的示例演示如何使用在TextPointer指定的文本容器中的第一个Run元素内部查找位置。The following example demonstrates how to use a TextPointer to find a position just inside of the first Run element in a specified text container.

// This method returns the position just inside of the first text Run (if any) in a 
// specified text container.
TextPointer FindFirstRunInTextContainer(DependencyObject container)
{
    TextPointer position = null;

    if (container != null){
        if (container is FlowDocument)
            position = ((FlowDocument)container).ContentStart;
        else if (container is TextBlock)
            position = ((TextBlock)container).ContentStart;
        else
            return position;
    }
    // Traverse content in forward direction until the position is immediately after the opening 
    // tag of a Run element, or the end of content is encountered.
    while (position != null)
    {
        // Is the current position just after an opening element tag?
        if (position.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart)
        {
            // If so, is the tag a Run?
            if (position.Parent is Run)
                break;
        }

        // Not what we're looking for; on to the next position.
        position = position.GetNextContextPosition(LogicalDirection.Forward);
    }
        
    // This will be either null if no Run is found, or a position just inside of the first Run element in the
    // specifed text container.  Because position is formed from ContentStart, it will have a logical direction
    // of Backward.
    return position;
} 
' This method returns the position just inside of the first text Run (if any) in a 
' specified text container.
Private Function FindFirstRunInTextContainer(ByVal container As DependencyObject) As TextPointer
    Dim position As TextPointer = Nothing

    If container IsNot Nothing Then
        If TypeOf container Is FlowDocument Then
            position = (CType(container, FlowDocument)).ContentStart
        ElseIf TypeOf container Is TextBlock Then
            position = (CType(container, TextBlock)).ContentStart
        Else
            Return position
        End If
    End If
    ' Traverse content in forward direction until the position is immediately after the opening 
    ' tag of a Run element, or the end of content is encountered.
    Do While position IsNot Nothing
        ' Is the current position just after an opening element tag?
        If position.GetPointerContext(LogicalDirection.Backward) = TextPointerContext.ElementStart Then
            ' If so, is the tag a Run?
            If TypeOf position.Parent Is Run Then
                Exit Do
            End If
        End If

        ' Not what we're looking for on to the next position.
        position = position.GetNextContextPosition(LogicalDirection.Forward)
    Loop

    ' This will be either null if no Run is found, or a position just inside of the first Run element in the
    ' specifed text container.  Because position is formed from ContentStart, it will have a logical direction
    ' of Backward.
    Return position
End Function

下面的示例使用TextPointer设施实现了一个简单的查找算法。The following example implements a simplistic find algorithm using TextPointer facilities.

// This method will search for a specified word (string) starting at a specified position.
TextPointer FindWordFromPosition(TextPointer position, string word)
{
    while (position != null)
    {
         if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
         {
             string textRun = position.GetTextInRun(LogicalDirection.Forward);

             // Find the starting index of any substring that matches "word".
             int indexInRun = textRun.IndexOf(word);
             if (indexInRun >= 0)
             {
                 position = position.GetPositionAtOffset(indexInRun);
                 break;
             }
         }
         else
            position = position.GetNextContextPosition(LogicalDirection.Forward);
     }

     // position will be null if "word" is not found.
     return position; 
}
' This method will search for a specified word (string) starting at a specified position.
Private Function FindWordFromPosition(ByVal position As TextPointer, ByVal word As String) As TextPointer
    Do While position IsNot Nothing
        If position.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then
            Dim textRun As String = position.GetTextInRun(LogicalDirection.Forward)

            ' Find the starting index of any substring that matches "word".
            Dim indexInRun As Integer = textRun.IndexOf(word)
            If indexInRun >= 0 Then
                position = position.GetPositionAtOffset(indexInRun)
                Exit Do
            End If
        Else
            position = position.GetNextContextPosition(LogicalDirection.Forward)
        End If
    Loop

    ' position will be null if "word" is not found.
    Return position
End Function

注解

TextPointer类引入了以下术语:The TextPointer class introduces the following terminology:

  • 位置-本质上, TextPointer始终指向内容中的位置Position - Inherently, a TextPointer always points to a position in content. 此类位置要么位于内容中的字符之间,要么位于为内容定义结构的流内容元素标记之间。Such positions either fall between characters in the content, or between flow content element tags that define structure for the content.

  • 当前位置-因为TextPointer始终指示位置,并且可以TextPointer通过执行的许多操作都是相对于TextPointer当前指向的位置的,因此,只需引用由TextPointer指示的位置作为当前位置Current Position - Because a TextPointer always indicates a position, and because many of the operations that can be performed through a TextPointer are relative to the position currently pointed to by the TextPointer, it makes sense to simply refer to the position indicated by a TextPointer as the current position.

  • 插入位置-插入位置是一个可以在其中添加新内容的位置,而不会破坏关联内容的任何语义规则。Insertion Position - An insertion position is a position where new content may be added without breaking any semantic rules for the associated content. 在实际操作中,插入位置在内容中的任何位置都可以定位插入点。In practice, an insertion position is anywhere in content where a caret may be positioned. 不是插入位置的TextPointer有效位置的一个示例是两个相邻Paragraph标记之间的位置(即,上一段的结束标记与下一段的开始标记之间)的位置。An example of a valid TextPointer position that is not an insertion position is the position between two adjacent Paragraph tags (that is, between the closing tag of the preceding paragraph and the opening tag of the next paragraph).

  • 符号-出于涉及符号的TextPointer操作的目的,以下任一操作都被视为符号Symbol - For the purposes of TextPointer operations that involve symbols, any of the following is considered to be a symbol:

  • 文本容器-文本容器是窗体上的流内容最终边框的元素;指示的位置TextPointer始终位于文本容器中。Text Container - A text container is the element that forms the ultimate border for the flow content at hand; the position indicated by a TextPointer always falls within a text container. 当前,文本容器必须FlowDocumentTextBlock或。Currently, a text container must be either a FlowDocument or a TextBlock. 一般而言,不支持不同TextPointer文本容器中的实例之间的操作。Generally speaking, operations between TextPointer instances in different text containers are not supported.

  • 文档-文本容器中的内容称为文档,如IsInSameDocument方法和DocumentStartDocumentEnd属性中所示。Document - The content in a text container is referred to as a document, as in the IsInSameDocument method and the DocumentStart and DocumentEnd properties.

类旨在帮助遍历和操作流内容元素所表示的内容; 通常Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF)情况下,此类元素派生自TextElementTextPointerThe TextPointer class is intended to facilitate traversal and manipulation of content that is represented by Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) flow content elements; in general, such elements derive from TextElement. TextPointer有助于执行以下操作:Some of the operations that TextPointer facilitates include the following:

TextPointer对象的位置和LogicalDirection指示是不可变的。The position and LogicalDirection indicated by a TextPointer object are immutable. 编辑或修改内容时,由指示TextPointer的位置不会相对于周围文本更改; 而是相对于内容的开头位置的偏移量进行调整,以反映中的新相对位置。content.When content is edited or modified, the position indicated by a TextPointer does not change relative to the surrounding text; rather the offset of that position from the beginning of content is adjusted correspondingly to reflect the new relative position in content. 例如, TextPointer指示给定段落开头位置的位置将继续指向该段的开头,即使在段落之前或之后插入或删除内容也是如此。For example, a TextPointer that indicates a position at the beginning of a given paragraph continues to point to the beginning of that paragraph even when content is inserted or deleted before or after the paragraph.

TextPointer类不提供任何公共构造函数。The TextPointer class does not provide any public constructors. TextPointer实例是使用其他对象(包括其他TextPointer对象)的属性或方法创建的。An instance of TextPointer is created by using properties or methods of other objects (including other TextPointer objects). 下面的列表提供了一些方法和属性的示例,这些示例创建并TextPointer返回。The following list provides a few examples of methods and properties that create and return a TextPointer. 此列表并不详尽:This list is not exhaustive:

属性

DocumentEnd

获取一个 TextPointer,它指向文本容器中与当前位置相关联的内容的结束位置。Gets a TextPointer at the end of content in the text container associated with the current position.

DocumentStart

获取一个 TextPointer,它指向文本容器中与当前位置相关联的内容的开始位置。Gets a TextPointer at the beginning of content in the text container associated with the current position.

HasValidLayout

获取一个值,该值指示与当前位置相关联的文本容器是否具有有效(最新)的布局。Gets a value that indicates whether the text container associated with the current position has a valid (up-to-date) layout.

IsAtInsertionPosition

获取一个值,该值指示当前位置是否是一个插入位置。Gets a value that indicates whether the current position is an insertion position.

IsAtLineStartPosition

获取一个值,该值指示当前位置是否位于行的开始处。Gets a value that indicates whether the current position is at the beginning of a line.

LogicalDirection

获取与当前位置相关联的逻辑方向,用于消除与当前位置相关联内容的不确定性。Gets the logical direction associated with the current position which is used to disambiguate content associated with the current position.

Paragraph

获取涵盖当前位置的段落(如果有)。Gets the paragraph that scopes the current position, if any.

Parent

获取涵盖当前位置的逻辑父项。Gets the logical parent that scopes the current position.

方法

CompareTo(TextPointer)

对当前 TextPointer 和第二个指定 TextPointer 所表示位置的顺序进行比较。Performs an ordinal comparison between the positions specified by the current TextPointer and a second specified TextPointer.

DeleteTextInRun(Int32)

从当前 TextPointer 指示的位置开始删除指定数目的字符。Deletes the specified number of characters from the position indicated by the current TextPointer.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetAdjacentElement(LogicalDirection)

返回在指定逻辑方向上的边界正好位于当前 TextPointer 处的元素(如果有)。Returns the element, if any, that borders the current TextPointer in the specified logical direction.

GetCharacterRect(LogicalDirection)

返回在指定逻辑方向上的边界正好位于当前 Rect 处的内容的边框 (TextPointer)。Returns a bounding box (Rect) for content that borders the current TextPointer in the specified logical direction.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetInsertionPosition(LogicalDirection)

返回一个指向指定逻辑方向上的最近插入位置的 TextPointerReturns a TextPointer to the closest insertion position in the specified logical direction.

GetLineStartPosition(Int32)

返回一个 TextPointer,它指向相对于当前 TextPointer 指定的行的开始位置。Returns a TextPointer to the beginning of a line that is specified relative to the current TextPointer.

GetLineStartPosition(Int32, Int32)

返回一个指向相对于当前 TextPointer 所指定行的开始位置的 TextPointer,并报告跳过的行数。Returns a TextPointer to the beginning of a line that is specified relative to the current TextPointer, and reports how many lines were skipped.

GetNextContextPosition(LogicalDirection)

返回一个指向指定逻辑方向上的下一个符号的指针。Returns a pointer to the next symbol in the specified logical direction.

GetNextInsertionPosition(LogicalDirection)

返回一个指向指定逻辑方向上的下一个插入位置的 TextPointerReturns a TextPointer to the next insertion position in the specified logical direction.

GetOffsetToPosition(TextPointer)

返回当前 TextPointer 与第二个指定 TextPointer 之间的符号数。Returns the count of symbols between the current TextPointer and a second specified TextPointer.

GetPointerContext(LogicalDirection)

返回在指定逻辑方向上与当前 TextPointer 相邻的内容的类别指示标志。Returns a category indicator for the content adjacent to the current TextPointer in the specified logical direction.

GetPositionAtOffset(Int32)

返回一个 TextPointer,它指向从当前 TextPointer 的开始位置计算的由指定偏移量(以符号数为单位)指示的位置。Returns a TextPointer to the position indicated by the specified offset, in symbols, from the beginning of the current TextPointer.

GetPositionAtOffset(Int32, LogicalDirection)

返回一个 TextPointer,它指向从当前 TextPointer 的开始位置沿指定方向计算的由指定偏移量(以符号数为单位)指示的位置。Returns a TextPointer to the position indicated by the specified offset, in symbols, from the beginning of the current TextPointer and in the specified direction.

GetTextInRun(LogicalDirection)

返回一个字符串,其中包含在指定逻辑方向上与当前 TextPointer 相邻的任何文本。Returns a string containing any text adjacent to the current TextPointer in the specified logical direction.

GetTextInRun(LogicalDirection, Char[], Int32, Int32)

将从指定方向上的任何相邻文本中提取的指定了最大数量的字符复制到由调用方提供的字符数组中。Copies the specified maximum number of characters from any adjacent text in the specified direction into a caller-supplied character array.

GetTextRunLength(LogicalDirection)

返回当前 TextPointer 与指定逻辑方向上的下一个非文本符号之间的 Unicode 字符数。Returns the number of Unicode characters between the current TextPointer and the next non-text symbol, in the specified logical direction.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InsertLineBreak()

在当前位置插入一个换行符。Inserts a line break at the current position.

InsertParagraphBreak()

在当前位置插入一个分段符。Inserts a paragraph break at the current position.

InsertTextInRun(String)

将指定文本插入到文本 Run 中的当前位置。Inserts the specified text into the text Run at the current position.

IsInSameDocument(TextPointer)

指示指定位置与当前位置是否位于相同的文本容器内。Indicates whether the specified position is in the same text container as the current position.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

此类型或成员支持 Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) 基础结构,但不可直接在代码中使用。This type or member supports the Windows Presentation Foundation (WPF)Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

适用于

另请参阅