I am loading quite a lot of rich text into a RichTextBox
(WPF) and I want to scroll to the end of content:
richTextBox.Document.Blocks.Add(...)
richTextBox.UpdateLayout();
richTextBox.ScrollToEnd();
This doesn't work, ScrollToEnd
is executed when the layout is not finished, so it doesn't scroll to the end, it scrolls to around the first third of the text.
Is there a way to force a wait until the RichTextBox
has finished its painting and layout operations so that ScrollToEnd
actually scrolls to the end of the text?
Thanks.
Stuff that doesn't work:
EDIT:
I have tried the LayoutUpdated
event but it's fired immediately, same problem: the control is still laying out more text inside the richtextbox when it's fired so even a ScrollToEnd
there doesn't work...
I tried this:
richTextBox.Document.Blocks.Add(...)
richTextBoxLayoutChanged = true;
richTextBox.UpdateLayout();
richTextBox.ScrollToEnd();
and inside the richTextBox.LayoutUpdated
event handler:
if (richTextBoxLayoutChanged)
{
richTextBoxLayoutChanged = false;
richTextBox.ScrollToEnd();
}
The event is fired correctly but too soon, the richtextbox is still adding more text when it's fired, layout is not finished so ScrollToEnd
fails again.
EDIT 2: Following on dowhilefor's answer: MSDN on InvalidateArrange says
After the invalidation, the element will have its layout updated, which will occur asynchronously unless subsequently forced by UpdateLayout.
Yet even
richTextBox.InvalidateArrange();
richTextBox.InvalidateMeasure();
richTextBox.UpdateLayout();
does NOT wait: after these calls the richtextbox is still adding more text and laying it out inside itself asynchronously. ARG!
I have had a related situation: I have a print preview dialog that creates a fancy rendering. Normally, the user will click a button to actually print it, but I also wanted to use it to save an image without user involvement. In this case, creating the image has to wait until the layout is complete.
I managed that using the following:
Dispatcher.Invoke(new Action(() => {SaveDocumentAsImage(....);}), DispatcherPriority.ContextIdle);
The key is the DispatcherPriority.ContextIdle
, which waits until background tasks have completed.
Edit: As per Zach's request, including the code applicable for this specific case:
Dispatcher.Invoke(() => { richTextBox.ScrollToEnd(); }), DispatcherPriority.ContextIdle);
I should note that I'm not really happy with this solution, as it feels incredibly fragile. However, it does seem to work in my specific case.