TextPointer.IsInSameDocument(TextPointer) 方法

定義

指出指定的位置是否位於與目前位置相同的文字容器中。

public:
 bool IsInSameDocument(System::Windows::Documents::TextPointer ^ textPosition);
public bool IsInSameDocument(System.Windows.Documents.TextPointer textPosition);
member this.IsInSameDocument : System.Windows.Documents.TextPointer -> bool
Public Function IsInSameDocument (textPosition As TextPointer) As Boolean

參數

textPosition
TextPointer

一個 TextPointer 指定要與當前位置比較的局面。

傳回

true若表示textPosition位置與當前位置相同,則表示位置位於同一文字容器中;否則,。 false

例外狀況

textPositionnull

範例

以下範例展示了此方法的應用。 範例使用該 IsInSameDocument 方法檢查指定位置是否 TextPointer 位於兩個其他指定 TextPointer 實例之間,且無法保證三個位置都屬於同一文字容器。

// This method first checks for compatible text container scope, and then checks whether
// a specified position is between two other specified positions.
bool IsPositionContainedBetween(TextPointer positionToTest, TextPointer start, TextPointer end)
{
    // Note that without this check, an exception will be raised by CompareTo if positionToTest 
    // does not point to a position that is in the same text container used by start and end.
    //
    // This test also implicitely indicates whether start and end share a common text container.
    if (!positionToTest.IsInSameDocument(start) || !positionToTest.IsInSameDocument(end)) 
        return false;
    
    return start.CompareTo(positionToTest) <= 0 && positionToTest.CompareTo(end) <= 0;
}
' This method first checks for compatible text container scope, and then checks whether
' a specified position is between two other specified positions.
Private Function IsPositionContainedBetween(ByVal positionToTest As TextPointer, ByVal start As TextPointer, ByVal [end] As TextPointer) As Boolean
    ' Note that without this check, an exception will be raised by CompareTo if positionToTest 
    ' does not point to a position that is in the same text container used by start and end.
    '
    ' This test also implicitely indicates whether start and end share a common text container.
    If (Not positionToTest.IsInSameDocument(start)) OrElse (Not positionToTest.IsInSameDocument([end])) Then
        Return False
    End If

    Return start.CompareTo(positionToTest) <= 0 AndAlso positionToTest.CompareTo([end]) <= 0
End Function

備註

大多數涉及多個 TextPointer 實例的操作只有在該實例顯示的位置位於相同文字容器範圍內時才有效。 例如 CompareTo ,和 GetOffsetToPosition 方法不能與 a TextPointer 搭配到與當前位置相關聯的文字容器以外的位置。 使用此方法驗證指定的 TextPointer 操作是否與當前位置相容。

適用於