我想在 wpf richtextbox 中获取第一个字符索引

i want to getting first character index at line, in wpf richtextbox

当我输入行号时,我想知道该行第一个字符的索引。

每次点击按钮 在 richtextbox1 上打印文本。

我想知道输出行第一个字符的索引。

这是我的代码:

private int GetTextPositionAndLength(int position, int lineIndex, out int length)
{
    int richtTextLineIndex = GetFirstCharIndexFromLine(lineIndex);
    int index = 0;
    length = 0;
    return index + richtTextLineIndex;
}

private int GetFirstCharIndexFromLine(int lineIndex)
{
    // What should I enter the code?
    int index = 0;
    return index;
}

帮助我

    private int GetFirstCharIndexFromLine(int lineIndex)
    {
        int index = 0;

        var rtb = yourRichTextBox;

        TextRange textRange = new TextRange(
            rtb.Document.ContentStart,
            rtb.Document.ContentEnd
        );

        var alltext = textRange.Text;

        string[] lines = alltext.Replace("\n", "").Split('\r');

        if (lineIndex > lines.Count())
            throw new ArgumentOutOfRangeException("lineIndex");

        for (int i = 0; i < lineIndex; i++)
        {
            index += lines[i].Length;
        }

        return index;
    }

希望,有帮助