繁体   English   中英

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

[英]Set Caret/Cursor Position in RichTextBox - WPF

如何在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;

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

假设rtb是RichTextBox的名称,具有不同的块和内联 ,您可以通过以下方式在文档的开头设置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);

请记住设置焦点,以便光标出现在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-你-移动- RichTextBox中,脱字上调或者下调单行?论坛= WPF

myRichTextBox.Focus();
EditingCommands.MoveUpByLine.Execute(null, myRichTextBox);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM