RichTextBox.SelectionStart 找不到

RichTextBox.SelectionStart could not be found

是否有特殊原因导致我的代码不允许我以编程方式在 RichTextBox 中设置 SelectionStart 和 SelectionLength?

            text = System.IO.File.ReadAllText(path);
            prompterText.AppendText(File.ReadAllText(@path));
            prompterText.FontSize = textSize;
            prompterText.HorizontalAlignment = HorizontalAlignment.Center;
            prompterText.Focus();

            this.prompterText.SelectionStart = 0;
            this.prompterText.SelectionLength = 50;
            this.prompterText.SelectionBrush = System.Windows.Media.Brushes.Aqua ;

在选择开始行时,它告诉我“RichTextBox 不包含 SelectionStart 的定义,并且找不到接受 RichTextBox 作为第一个参数的扩展方法”。

这很奇怪,因为我发现很多代码示例在 RichTextBoxes 上使用完全相同的行。

    <ScrollViewer x:Name="scroller" Margin="0">
        <RichTextBox x:Name="prompterText" Margin="10" IsReadOnly="False"/>
    </ScrollViewer>

在 XAML 代码中,我将 IsReadOnly 设置为 false 以确保我有访问权限,但问题仍然存在。

我的意图是在提示符类型 window 中选择文本 运行 以设置特定的 reader 速度。

RichTextBox SelectionStart 属性 仅在 winforms 中可用。对于 WPF,我们需要使用 TextPointer 来进行选择。参考下面的代码。

 prompterText.AppendText("1111111111111111111111111111111111111111111111111111111111111111111111");           
        prompterText.HorizontalAlignment = HorizontalAlignment.Center;
        prompterText.Focus();
        TextPointer text = prompterText.Document.ContentStart;
        while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
        {
            text = text.GetNextContextPosition(LogicalDirection.Forward);
        }
        TextPointer startPos = text.GetPositionAtOffset(0);
        TextPointer endPos = text.GetPositionAtOffset(10);            
        prompterText.Selection.Select(startPos, endPos);
        this.prompterText.SelectionBrush = System.Windows.Media.Brushes.Aqua;