共用方式為


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,指定要與目前位置進行比較的位置。

傳回

如果目前 TextPointerposition 前面,則為 -1;如果位置相同,則為 0;如果目前 TextPointerposition 後面,則為 +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 指定的位置。

適用於

另請參閱