小编Mar*_*air的帖子

GetPositionAtOffset仅相当于文本?

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

C#中的动机示例:

TextRange GetRange(RichTextBox rtb, int startIndex, int length) {
    TextPointer startPointer = rtb.Document.ContentStart.GetPositionAtOffset(startIndex);
    TextPointer endPointer = startPointer.GetPositionAtOffset(length);
    return new TextRange(startPointer, endPointer);
}
Run Code Online (Sandbox Code Playgroud)

编辑:直到现在我以这种方式"解决"它

public static TextPointer GetInsertionPositionAtOffset(this TextPointer position, int offset, LogicalDirection direction)
{
    if (!position.IsAtInsertionPosition) position = position.GetNextInsertionPosition(direction);
    while (offset > 0 && position != null)
    {
        position = position.GetNextInsertionPosition(direction);
        offset--;
        if (Environment.NewLine.Length == 2 && position != null && position.IsAtLineStartPosition) offset --; 
    }
    return position;
}
Run Code Online (Sandbox Code Playgroud)

wpf richtextbox wpf-controls

6
推荐指数
1
解决办法
1107
查看次数

标签 统计

richtextbox ×1

wpf ×1

wpf-controls ×1