TextPointer.CompareTo(TextPointer) メソッド

定義

現在の TextPointer と指定された 2 つ目の 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

戻り値

現在の TextPointerposition の前である場合は -1。両位置が同じ場合は 0。現在の TextPointerposition の後である場合は +1。

例外

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位置に従っていることを示します。

適用対象

こちらもご覧ください