在 RichTextBox 中设置 Caret/Cursor 位置 - WPF
Set Caret/Cursor Position in RichTextBox - WPF
如何在 WPF 中设置 RichTextBox 中的 caret/cursor 位置?
我用了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;
记得设置焦点以便光标出现在 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设置caretPosbackward / forward
和您要移动的位移量:
int displacement = 8;
// Set the TextPointer 8 displacement backward.
caretPos = caretPos.GetPositionAtOffset(displacement, LogicalDirection.Backward);
.
例子,你可以把它粘贴到一个空Window的构造函数中来测试:
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;
}
}
.
结果:
(我把window移到中间防止残影)
.
其他补充:
如果你想将 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);
How to set caret/cursor position in RichTextBox in WPF?
假设rtb
是你的RichTextBox的名称,与Blocks and Inlines不同,你可以通过以下方式设置文档开头的插入符号:
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
中实现的不同方法,您可以使用它们到达 document/Blocks/Inlines 的不同部分:
rtb.CaretPosition = rtb.CaretPosition.GetLineStartPosition(0);
rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(2);
有关更多方法和更多信息,请参阅 link。
最后,您可能想使用 BringIntoView
在块或内联中实现的方法:
blk.BringIntoView();
r.BringIntoView();
并设置键盘焦点,以查看插入符的 blinking:
Keyboard.Focus(rtb);
如何在 WPF 中设置 RichTextBox 中的 caret/cursor 位置?
我用了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;
记得设置焦点以便光标出现在 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设置caretPosbackward / forward
和您要移动的位移量:
int displacement = 8;
// Set the TextPointer 8 displacement backward.
caretPos = caretPos.GetPositionAtOffset(displacement, LogicalDirection.Backward);
.
例子,你可以把它粘贴到一个空Window的构造函数中来测试:
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;
}
}
.
结果:
(我把window移到中间防止残影)
.
其他补充:
如果你想将 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);
How to set caret/cursor position in RichTextBox in WPF?
假设rtb
是你的RichTextBox的名称,与Blocks and Inlines不同,你可以通过以下方式设置文档开头的插入符号:
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
中实现的不同方法,您可以使用它们到达 document/Blocks/Inlines 的不同部分:
rtb.CaretPosition = rtb.CaretPosition.GetLineStartPosition(0);
rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(2);
有关更多方法和更多信息,请参阅 link。
最后,您可能想使用 BringIntoView
在块或内联中实现的方法:
blk.BringIntoView();
r.BringIntoView();
并设置键盘焦点,以查看插入符的 blinking:
Keyboard.Focus(rtb);