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 указывающее позицию для сравнения с текущей позицией.

Возвращаемое значение

-1, если текущий TextPointer предшествует position; 0, если расположения совпадают; +1, если текущий TextPointer следует position.

Исключения

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.

Применяется к

См. также раздел