title: RichTextBox and Inserting at Caret Positions
body: <p>Here is the deal: I have a RichTextBox control and it works fine. The problem is that there is a button "Insert Current DateTime" which adds/injects the current datetime into the RichTextBox. The user can enter the datetime anywhere where the caret is pointing. This involves complicated string manipulation and stuff. </p>
  
  <p>Any ideas how to get the current caret position. Whenever I get RichTextBox.CaretPositon it seems it is pointing to the start of the RichTextBox and not where the actual caret is.</p>
  
  <p><strong>UPDATE 1:</strong> </p>
  
  <p>The date time button click code: </p>
  
  <pre><code> private void DateTimeStampButton_Click(object sender, RoutedEventArgs e)
          {
              //TextRange tr = new TextRange(textBox.Selection.Start, textBox.Selection.End);
              var tr = new TextRange(textBox.Document.ContentStart, textBox.Document.ContentEnd);
  
              if(tr.Text.Length == 2)
              {
                  if(tr.Text == "\r\n")
                  {
                      tr.Text = tr.Text.TrimStart(new[] { '\r', '\n' }); 
                  }
              }
  
              textBox.CaretPosition.InsertTextInRun(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + ":  ");
  
              DateTimeStampButton.Focusable = false;
          }
  
   private void SharpRichTextBox_LostFocus(object sender, RoutedEventArgs e)
          {
              SetValue(TextProperty, Text);
  
              var binding = BindingOperations.GetBinding(this, TextProperty);
  
              if (binding == null) return;
  
              if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default || binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
              {
                  // if (TextProperty != null) BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
              }
          }
  
  
  
  
  
  
  public string Text
          {
              get
              {
                  var newValue = new TextRange(Document.ContentStart, Document.ContentEnd).Text.RemoveNewLineAndReturn(); 
                  return newValue; 
              }
              set
              {
                  if (!String.IsNullOrEmpty(value))
                  {
                      SetValue(TextProperty, value.RemoveNewLineAndReturn());
                      Document.Blocks.Clear(); 
                      Document.Blocks.Add(new Paragraph(new Run(value))); 
                      OnPropertyChanged("Text"); 
                  }
              }
          }
  </code></pre>
  
  <p><strong>UPDATE 2:</strong></p>
  
  <p>Turned out the problem was with the DateTime button being Focusable. I turned it to be not focusable and it worked as expected. When focus was lost on the RichTextBox it was resetting the caret position. It happened only once since in the code the btn_DateTime was dynamically being set as Focusable = false. I placed Focusable = false in XAML and everything worked fine from the start. </p>