I am having prohibitive performance issues with WPF's RichTextBox
on WPF forms.
So what I trying now is to replace all rich text controls with TextBlock
s, and replace the TextBlock
with a RichTextBox
when the user clicks.
To achieve this, I made a LazyRichTextBox
like this.
class LazyRichTextBox : UserControl
{
// lots of detail skipped
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{
if (!(Content is RichTextBox))
{
var box = new RichTextBox { Document = FlowDocumentHtmlConverter.ReplaceDocContent(RichText) };
Content = box;
}
base.OnPreviewMouseDown(e);
}
}
The good news is that this works to some extent: I now get an editable textbox. But what does not work is that it does not get the caret in the correct location. The control does not even get the focus from the mouseclick, the user must click again before he/she can start typing.
Now, magically replacing the control on a click may not be the only solution. I could also try to figure out the clicked character position on the TextBlock
, then create the RichTextBox
and set the focus and caret accordingly. Is that possible?
For the record: I am aware that there's more work to be done: I need to make sure that the TextBlocks appear identical to the RichTextBox appearing later, that tab behavior needs to be handled. I am now focusing on getting the Click scenario as good as possible.
Update: Thanks for the comments. I have very limited rich text, so I know I can emulate them with TextBlocks. The performance issue is due to suspected memory leak, causing a 2GB of memory footprint after two hours of use. I am using RichTextBox/FlowDocument because I don't know any other way to edit text with inline formatting, would be interested in alternatives.