Wpf GetPositionAtOffset是否仅用于文本?

Wpf GetPositionAtOffset是否仅用于文本?,wpf,wpf-controls,richtextbox,Wpf,Wpf Controls,Richtextbox,是否有一个相当于只计算文本插入位置而不是所有符号的GetPositionAtOffset()解决方案 C#中的动机示例: TextRange GetRange(RichTextBox rtb,int startIndex,int length){ TextPointer=rtb.Document.ContentStart.GetPositionAtOffset(startIndex); TextPointer endPointer=startPointer.GetPositionAtOffset

是否有一个相当于只计算文本插入位置而不是所有符号的
GetPositionAtOffset()
解决方案

C#中的动机示例:

TextRange GetRange(RichTextBox rtb,int startIndex,int length){
TextPointer=rtb.Document.ContentStart.GetPositionAtOffset(startIndex);
TextPointer endPointer=startPointer.GetPositionAtOffset(长度);
返回新的文本范围(起始点、终结点);
}
编辑:直到现在我都是这样“解决”的

公共静态文本指针GetInsertionPositionAtOffset(此文本指针位置,int偏移,LogicalDirection方向)
{
如果(!position.IsAtInsertionPosition)position=position.getNextInertionPosition(方向);
while(偏移量>0&&position!=null)
{
位置=位置。GetNextInsertionPosition(方向);
偏移量--;
如果(Environment.NewLine.Length==2&&position!=null&&position.IsAtLineStartPosition)偏移--;
}
返回位置;
}

据我所知,没有。我的建议是为此创建自己的GetPositionAtOffset方法。您可以使用以下方法检查TextPointer与哪个PointerContext相邻:

TextPointer.GetPointerContext(LogicalDirection);
要获取指向不同PointerContext的下一个文本指针,请执行以下操作:

TextPointer.GetNextContextPosition(LogicalDirection);
我在最近的一个项目中使用的一些示例代码,通过循环直到找到一个,确保指针上下文为Text类型。您可以在实现中使用此选项,并跳过偏移增量(如果发现):

// for a TextPointer start

while (start.GetPointerContext(LogicalDirection.Forward) 
                             != TextPointerContext.Text)
{
    start = start.GetNextContextPosition(LogicalDirection.Forward);
    if (start == null) return;
}

希望您能利用这些信息。

很长一段时间都无法找到有效的解决方案。 下一段代码以最高的性能在我的情况下工作。希望它也能帮助别人

TextPointer startPos = rtb.Document.ContentStart.GetPositionAtOffset(searchWordIndex, LogicalDirection.Forward);
startPos = startPos.CorrectPosition(searchWord, FindDialog.IsCaseSensitive);
if (startPos != null)
{
    TextPointer endPos = startPos.GetPositionAtOffset(textLength, LogicalDirection.Forward);
    if (endPos != null)
    {
         rtb.Selection.Select(startPos, endPos);
    }
}

public static TextPointer CorrectPosition(this TextPointer position, string word, bool caseSensitive)
{
   TextPointer start = null;
   while (position != null)
   {
       if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
       {
           string textRun = position.GetTextInRun(LogicalDirection.Forward);

           int indexInRun = textRun.IndexOf(word, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
           if (indexInRun >= 0)
           {
               start = position.GetPositionAtOffset(indexInRun);
               break;
           }
       }

       position = position.GetNextContextPosition(LogicalDirection.Forward);
   }

   return start; }

美好的直到现在才知道TextPointerContext:)很乐意帮忙。如果你还需要什么,请告诉我!很好的狩猎。哇,有人真的不想使用RichTextBox。。谢谢