如何在WPF中的RichTextBox中设置插入符/光标位置?
我使用MSDN CaretPosition中的代码,但似乎光标无法设置?
// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);
// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;
// Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd;
// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;
答案 0 :(得分:11)
如何在WPF中的RichTextBox中设置插入符/光标位置?
假设rtb
是您的RichTextBox的名称,使用不同的Blocks and Inlines,您可以在文档的开头设置Caret:
rtb.CaretPosition = rtb.CaretPosition.DocumentStart;
或在其结尾处:
rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;
另一方面,假设您有一个特定的段落或块,例如:
Block blk = rtb.Document.Blocks.ElementAt(1);
您可以将插入符号设置为其开头
rtb.CaretPosition = blk.ContentStart;
或其结束
rtb.CaretPosition = blk.ContentEnd;
或者如果您有特定的内联,例如
Run r = ((Paragraph)rtb.Document.Blocks.ElementAt(0)).Inlines.ElementAt(1) as Run;
您也可以使用
rtb.CaretPosition = r.ContentStart;
rtb.CaretPosition = r.ContentEnd;
当然,如果您使用从右到左和从左到右文本的复杂段落,您可能需要考虑
rtb.CaretPosition = blk.ElementStart;
rtb.CaretPosition = blk.ElementEnd;
还要注意TextPointer
中实现的不同方法,您可以使用这些方法来覆盖文档/块/内联的不同部分:
rtb.CaretPosition = rtb.CaretPosition.GetLineStartPosition(0);
rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(2);
有关更多方法和更多信息,请参阅链接。
最后,您可能希望使用块或内联中实现的BringIntoView
方法:
blk.BringIntoView();
r.BringIntoView();
并设置键盘焦点,以查看Caret的闪烁:
Keyboard.Focus(rtb);
答案 1 :(得分:3)
请记住设置焦点,以便光标显示在RichTextBox中:
// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);
//****SET FOCUS****
rtb.Focus();
// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;
// Set the TextPointer to the end of the current document.
caretPos = caretPos.DocumentEnd;
// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;
除了设置为文档结束外,您还可以使用GetPositionAtOffset设置caretPos backward / forward
以及您想要移动的位移量:
int displacement = 8;
// Set the TextPointer 8 displacement backward.
caretPos = caretPos.GetPositionAtOffset(displacement, LogicalDirection.Backward);
示例,您可以将其粘贴到空窗口的构造函数中进行测试:
public RichTbxFlowDocumentTest()
{
InitializeComponent();
// Create a new FlowDocument, and add 3 paragraphs.
FlowDocument flowDoc = new FlowDocument();
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
flowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
// Set the FlowDocument to be the content for a new RichTextBox.
RichTextBox rtb = new RichTextBox(flowDoc);
//Add RichTextBox and Button with setting cursor method to a new StackPanel
StackPanel s = new StackPanel();
Button button = new Button() { Content = "Set Cursor Pos" };
button.Click += (sender, e) =>
{
//SET FOCUS
rtb.Focus();
// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;
//Set amount of displacement
int displacement = 6;
// Set the TextPointer 6 displacement backward
caretPos = caretPos.GetPositionAtOffset(displacement, LogicalDirection.Backward);
// Specify the new caret position to RichTextBox
rtb.CaretPosition = caretPos;
};
s.Children.Add(button);
s.Children.Add(rtb);
this.Content = s;
}
}
结果:
(我在中间移动窗口以防止残像)
其他补充:
如果要将RichTextBox插入符号向上或向下移动一行,您可以看到 https://social.msdn.microsoft.com/Forums/vstudio/en-US/8c34e7b1-91ed-4b11-979d-d18b28a71f6f/how-do-you-move-richtextbox-caret-up-or-down-one-line?forum=wpf
myRichTextBox.Focus();
EditingCommands.MoveUpByLine.Execute(null, myRichTextBox);