【发布时间】:2017-06-29 06:11:25
【问题描述】:
我试图将插入符号放在 Run 内的 RichTextBox 中,以使用户能够继续使用相同的格式输入。在这个 Run 之后,还有另一个不同格式的 Run。
这里有一些代码可以告诉你我的意思:
private void Button_Click(object sender, RoutedEventArgs e)
{
Paragraph p = new Paragraph();
// Add the new Paragraph to the RichTextBox flowdocument.
rtb1.Document.Blocks.Add(p);
// Create the first Run.
Run r1 = new Run("This is a ");
// Create the second Run.
Run r2 = new Run("test");
r2.TextDecorations = TextDecorations.Underline;
// Add the new Runs to the Paragraph.
p.Inlines.Add(r1);
p.Inlines.Add(r2);
// Place the caret at the end of the first Run, and give focus to the RichTextBox.
rtb1.CaretPosition = r1.ContentEnd;
rtb1.Focus();
}
如果我继续输入,新的输入将得到与第二次运行相同的格式。
我也尝试将 CaretPosition 设置为
new TextRange(r1.ElementEnd, r1.ElementEnd).Start
希望因为它的逻辑方向设置为向后它会起作用,但无济于事。
有没有办法在第一个 Run 中设置 CaretPosition?
【问题讨论】:
标签: c# wpf richtextbox caret flowdocument