在 RichTextBox 中设置插入符号/光标位置 - WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42429765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 14:01:16  来源:igfitidea点击:

Set Caret/Cursor Position in RichTextBox - WPF

c#wpfrichtextbox

提问by yu yang Jian

How to set caret/cursor position in RichTextBox in WPF?

如何在 WPF 中的 RichTextBox 中设置插入符号/光标位置?

I use the code in MSDN CaretPositionbut it seems the cursor can't be set?

我使用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;

回答by Ron

How to set caret/cursor position in RichTextBox in WPF?

如何在 WPF 中的 RichTextBox 中设置插入符号/光标位置?

Assuming thatrtbis the name of your RichTextBox, with different Blocks and Inlines, you can set the Caret at the beginning of the document by:

假设这rtb是您的 RichTextBox 的名称,具有不同的Blocks 和 Inlines,您可以通过以下方式在文档的开头设置插入符号:

rtb.CaretPosition = rtb.CaretPosition.DocumentStart;

or at its end:

或在其末尾:

rtb.CaretPosition = rtb.CaretPosition.DocumentEnd;

On the other hand, assuming that you have a specific Paragraph or Block, such as:

另一方面,假设您有一个特定的段落或块,例如:

Block blk = rtb.Document.Blocks.ElementAt(1);

You can set caret to its start

您可以将插入符号设置为其开头

rtb.CaretPosition = blk.ContentStart;

or its end

或者它的结束

rtb.CaretPosition = blk.ContentEnd;

or if you have a specific Inline such as

或者如果您有特定的内联,例如

Run r = ((Paragraph)rtb.Document.Blocks.ElementAt(0)).Inlines.ElementAt(1) as Run;

You can also use

你也可以使用

rtb.CaretPosition = r.ContentStart;
rtb.CaretPosition = r.ContentEnd;

Of course, if you are working with Complex paragraph with both right-to-left and left-to-right text, you might need to consider

当然,如果您正在处理具有从右到左和从左到右文本的复杂段落,您可能需要考虑

rtb.CaretPosition = blk.ElementStart;
rtb.CaretPosition = blk.ElementEnd;

Also note different methods implemented in a TextPointer, which you can use to reach different parts of the document/Blocks/Inlines:

还要注意在 a 中实现的不同方法TextPointer,您可以使用它们来访问文档/块/内联的不同部分:

rtb.CaretPosition = rtb.CaretPosition.GetLineStartPosition(0);
rtb.CaretPosition = rtb.CaretPosition.GetPositionAtOffset(2);

See the link for more methods and more information.

有关更多方法和更多信息,请参阅链接。

At the end, you might want to use BringIntoViewmethod implemented in a Block or Inline:

最后,您可能希望使用BringIntoView在 Block 或 Inline 中实现的方法:

blk.BringIntoView();
r.BringIntoView();

and also setting the keyboard focus, to see the blinking of the Caret:

并设置键盘焦点,以查看插入符号的闪烁:

Keyboard.Focus(rtb);

回答by yu yang Jian

Remember to Set focusso that the cursor will appear in the RichTextBox:

请记住设置焦点,以便光标出现在 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;

.

.

In addition to Setting to Document End, You can use GetPositionAtOffsetto set caretPos backward / forwardand amount of displacement you want to move:

除了设置为文档结束之外,您还可以使用 GetPositionAtOffset设置 caretPosbackward / forward和要移动的位移量:

int displacement = 8;

// Set the TextPointer 8 displacement backward.
caretPos = caretPos.GetPositionAtOffset(displacement, LogicalDirection.Backward);

.

.

Example,you can paste it to the Constructor of an Empty Window to test:

例如,您可以将其粘贴到空窗口的构造函数中进行测试:

    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;
    }
}

.

.

Result:

结果:

(I move the window in the middle to prevent afterimage)

(我将窗口移到中间以防止残影)

enter image description here

在此处输入图片说明

.

.

Other Supplement:

其他补充:

If you want to move RichTextBox caret up or down one line, you can see 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

如果您想将 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);