TextPointer.CompareTo(TextPointer) Metodo

Definizione

Esegue un confronto ordinale tra le posizioni specificate dall'oggetto TextPointer corrente e un secondo oggetto TextPointer specificato.

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

Parametri

position
TextPointer

Oggetto TextPointer che specifica una posizione da confrontare con la posizione corrente.

Restituisce

Int32

-1 se l'oggetto TextPointer corrente precede position; 0 se le posizioni sono uguali; +1 se l'oggetto TextPointer corrente segue position.

Eccezioni

position specifica una posizione esterna al contenitore di testo associato alla posizione corrente.

Esempio

Nell'esempio seguente viene illustrato un utilizzo per questo metodo. Nell'esempio il CompareTo metodo viene usato insieme al GetInsertionPosition metodo per verificare se un oggetto specificato TextElement è vuoto.

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

Commenti

Il valore -1 indica che la posizione specificata dall'oggetto corrente TextPointer precede la posizione specificata da position. Il valore 0 indica che le posizioni indicate sono uguali. Un valore positivo +1 indica che la posizione specificata dall'oggetto corrente TextPointer segue la posizione specificata da position.

Si applica a

Vedi anche