TextPointer.CompareTo(TextPointer) Methode

Definition

Führt einen Ordinalvergleich zwischen den positionen aus, die durch den aktuellen TextPointer und einen zweiten angegebenen Wert angegeben wurden 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

Parameter

position
TextPointer

Ein TextPointer Wert, der eine Position angibt, die mit der aktuellen Position verglichen werden soll.

Gibt zurück

-1, wenn der aktuelle TextPointer Wert vorangestellt ist position; 0, wenn die Speicherorte identisch sind; +1, wenn der aktuelle TextPointer Wert folgt position.

Ausnahmen

position Gibt eine Position außerhalb des Textcontainers an, der der aktuellen Position zugeordnet ist.

Beispiele

Im folgenden Beispiel wird eine Verwendung für diese Methode veranschaulicht. Im Beispiel wird die CompareTo Methode zusammen mit der GetInsertionPosition Methode verwendet, um zu testen, ob ein angegebener TextElement Wert leer ist.

// 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.

Hinweise

Ein Wert von -1 gibt an, dass die durch die aktuelle TextPointer Position angegebene Position der angegebenen positionPosition vorangestellt ist. Ein Wert von 0 gibt an, dass die angegebenen Positionen gleich sind. Ein Wert von positivem +1 gibt an, dass die durch die aktuelle TextPointer Position angegebene Position der angegebenen Position folgt position.

Gilt für:

Weitere Informationen