是否有GetPositionAtOffset()
相当于只计算文本插入位置,而不是所有符号的漂亮解决方案?在C#GetPositionAtOffset仅适用于文本?
动机例如:
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);
}
编辑:到现在为止我 “解决” 这种方式
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;
}
哇哇有人真的不希望使用RichTextBox ..谢谢! – gjvdkamp 2014-02-22 09:29:45