I have a dynamic flow document displayed in a RichTextBox representing a conversation between two persons.
I am trying to add a feature to my software that will add a paragraph after any existing paragraphs where the caret is currently active (by using the tab key). I was able to achieve the insertion but one bug remains, I am unable to set the caret position to the start of the newly created paragraph. I have tried to focus on the newly created paragraph with no luck.
Here is some test code for the tab pressed event where it adds the new paragraph at the right location. ConversationXContent is a FlowDocument.
private void RichTextBox_KeyDown(object sender, KeyEventArgs e)
{
if(e != null && e.Key == Key.Tab)
{
Paragraph p = new Paragraph();
p.Tag = "NewParagraph";
p.SetResourceReference(Paragraph.StyleProperty, "CharacterViewStyle");
p.Inlines.Add("NEW PARAGRAPH");
ConversationXContent.Blocks.InsertAfter(ConversationX.CaretPosition.Paragraph, p);
}
}
After investigation and a few trials and errors i was able to solve my last bug with the following code that places the mouse caret to the start of the newly created paragraph. Hope this can help others