TextPointer.CompareTo(TextPointer) 方法

定义

对当前 TextPointer 和第二个指定 TextPointer 所表示位置的顺序进行比较。

public:
 int CompareTo(System::Windows::Documents::TextPointer ^ position);
public int CompareTo (System.Windows.Documents.TextPointer position);
member this.CompareTo : System.Windows.Documents.TextPointer -> int
Public Function CompareTo (position As TextPointer) As Integer

参数

position
TextPointer

一个 TextPointer,它指定要与当前位置进行比较的位置。

返回

如果当前 TextPointer 位于 position 之前,则为 -1;如果位置相同,则为 0;如果当前 TextPointer 位于 position 之后,则为 +1。

例外

position 指定位于与当前位置相关联的文本容器之外的某个位置。

示例

以下示例演示了此方法的用法。 在此示例中,方法 CompareTo 与 方法结合使用, GetInsertionPosition 以测试指定的 TextElement 是否为空。

// Tests to see if the specified TextElement is empty (has no printatble content).
bool IsElementEmpty(TextElement element)
{
    // Find starting and ending insertion positions in the element.
    // Inward-facing directions are used to make sure that insertion position
    // will be found correctly in case when the element may contain inline 
    // formatting elements (such as a Span or Run that contains Bold or Italic elements).
    TextPointer start = element.ContentStart.GetInsertionPosition(LogicalDirection.Forward);
    TextPointer end = element.ContentEnd.GetInsertionPosition(LogicalDirection.Backward);
     
    // The element has no printable content if its first and last insertion positions are equal.
    return start.CompareTo(end) == 0;
} // End IsEmptyElement method.
' Tests to see if the specified TextElement is empty (has no printatble content).
Private Function IsElementEmpty(ByVal element As TextElement) As Boolean
    ' Find starting and ending insertion positions in the element.
    ' Inward-facing directions are used to make sure that insertion position
    ' will be found correctly in case when the element may contain inline 
    ' formatting elements (such as a Span or Run that contains Bold or Italic elements).
    Dim start As TextPointer = element.ContentStart.GetInsertionPosition(LogicalDirection.Forward)
    Dim [end] As TextPointer = element.ContentEnd.GetInsertionPosition(LogicalDirection.Backward)

    ' The element has no printable content if its first and last insertion positions are equal.
    Return start.CompareTo([end]) = 0

End Function ' End IsEmptyElement method.

注解

值 -1 指示当前 TextPointer 指定的位置先于 指定 position的位置。 值为 0 表示所指示的位置相等。 正值 +1 指示当前 TextPointer 指定的位置后跟指定 position的位置。

适用于

另请参阅