title: Set Caret/Cursor Position in RichTextBox - WPF
body: <p>How to set caret/cursor position in RichTextBox in WPF?</p>
  
  <p>I use the code in <a href="https://msdn.microsoft.com/zh-tw/library/system.windows.controls.richtextbox.caretposition(v=vs.110).aspx" rel="noreferrer">MSDN CaretPosition</a> but it seems the cursor can't be set?</p>
  
  <pre><code>// 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;
  </code></pre>