TextPointer.CompareTo(TextPointer) Méthode

Définition

Effectuer une comparaison ordinale entre les positions spécifiées par le TextPointer actuel et une seconde spécifié par 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

Paramètres

position
TextPointer

Un TextPointer qui spécifie une position à comparer à la position actuelle.

Retours

Int32

-1 si le TextPointer actuel précède position ; 0 si les emplacements sont les mêmes ; +1 si le TextPointer actuel suit position.

Exceptions

position spécifie une position en dehors du conteneur de texte associé à la position actuelle.

Exemples

L’exemple suivant illustre une utilisation pour cette méthode. Dans l’exemple, la CompareTo méthode est utilisée conjointement avec la GetInsertionPosition méthode pour tester si un spécifié TextElement est vide.

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

Remarques

La valeur -1 indique que la position spécifiée par le courant TextPointer précède la position spécifiée par position. La valeur 0 indique que les positions indiquées sont égales. Une valeur positive +1 indique que la position spécifiée par le courant TextPointer suit la position spécifiée par position.

S’applique à

Voir aussi