是否有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