TextPointer.GetInsertionPosition(LogicalDirection) メソッド

定義

指定した論理方向の最も近い挿入位置への TextPointer を返します。

public:
 System::Windows::Documents::TextPointer ^ GetInsertionPosition(System::Windows::Documents::LogicalDirection direction);
public System.Windows.Documents.TextPointer GetInsertionPosition (System.Windows.Documents.LogicalDirection direction);
member this.GetInsertionPosition : System.Windows.Documents.LogicalDirection -> System.Windows.Documents.TextPointer
Public Function GetInsertionPosition (direction As LogicalDirection) As TextPointer

パラメーター

direction
LogicalDirection

最も近い挿入位置を検索する論理方向を指定する LogicalDirection 値のいずれか。

戻り値

TextPointer

指定した方向の最も近い挿入位置への TextPointer

この例では、メソッドを 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.

注釈

挿入位置は、関連するコンテンツのセマンティック ルールを破ることなく、新しいコンテンツを追加できる位置です。 実際には、挿入位置は、キャレットを配置できるコンテンツ内の任意の場所にあります。 挿入位置ではない有効な TextPointer 位置の例として、隣接する Paragraph 2 つのタグ間の位置 (つまり、前の段落の終了タグと次の段落の開始タグの間) があります。

既に TextPointer 有効な挿入位置を指しているが、空でない書式設定シーケンスの終了タグが指定した方向の位置に直接従う場合、このメソッドによって返される位置は、 TextPointer 書式設定シーケンスの閉じ直後に挿入位置を指すように調整されます。 たとえば、マークアップ シーケンスを考えてみましょう <Bold>a</Bold>b。 文字a b の間には 2 つの挿入位置があり、1 つは終了タグの前にあり、1 つは終了Bold``Boldタグの直後にあります。 GetInsertionPosition文字の直後aと終了Boldタグの前の位置に対して a TextPointer を呼び出し、a direction を指定すると、返されるTextPointer位置は、終了タグのForward後の文字bの直前の位置をBold指すように調整されます。 反対の論理方向で作業するときに、書式設定タグを開く場合にも同様の調整が行われます。 この方法は、同様の場合に挿入位置間のあいまいさを解消する手段を提供することを目的としています。

このメソッドは、構造タグのシーケンスが関係する場合に挿入ポイントを選択的に使用することもできます。 たとえば、段落タグを閉じる位置と開く位置にある場合、方向パラメーターを使用して、次の段落の先頭 (指定) または前の段落の末尾 (指定 LogicalDirection.Forwardにより) に最も近い挿入ポイントを LogicalDirection.Backward選択できます。

ポインターが既に挿入位置にあり、指定した direction位置に隣接する書式設定タグがない場合、返される TextPointer 位置は呼び出し元 TextPointerと同じ位置を指します。

が指す位置に対して有効な挿入位置が存在しない可能性があります TextPointer。 これは、空のテーブルやリストのように、参照先のコンテンツが構造的に不完全な場合に発生する可能性があります。 このような場合、このメソッドは、このメソッドの呼び出し元と同じ位置に単に TextPointer a TextPointer を返します。 このメソッドは常に有効な TextPointer値を返します。

適用対象

こちらもご覧ください