WPF中的RichTextBox没有.Lines属性?

Jac*_*ack 7 .net c# wpf winforms

是否有相当于WPF中winForms的.Lines?

我目前正在使用这个:

var textRange = new TextRange(TextInput.Document.ContentStart, TextInput.Document.ContentEnd);
string[] lines = textRange.Text.Split('\n');
Run Code Online (Sandbox Code Playgroud)

Raj*_*han 4

RichTextBox 是 Fl​​owDocument 类型,并且没有 Lines 属性。你正在做的事情似乎是一个很好的解决方案。您可能想使用IndexOf而不是 split。

您还可以添加扩展方法,如文章所示:

public static long Lines(this string s)
{
    long count = 1;
    int position = 0;
    while ((position = s.IndexOf('\n', position)) != -1)
        {
        count++;
        position++;         // Skip this occurance!
        }
    return count;
}
Run Code Online (Sandbox Code Playgroud)