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 値の 1 つ。

返品

指定した方向の最も近い挿入位置への 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 位置の例は、隣接する 2 つの Paragraph タグ間 (つまり、前の段落の終了タグと次の段落の開始タグの間) の位置です。

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

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

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

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

適用対象

こちらもご覧ください