簡體   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