I am loading quite a lot of rich text into a RichTextBox (WPF) and I want to scroll to the end of the content:
richTextBox.Document.Blocks.Add(...) richTextBox.UpdateLayout(); richTextBox.ScrollToEnd();
This does not work, ScrollToEnd is executed when the layout is not finished yet, so it does not scroll to the end, it scrolls around the first third of the text.
Is there a way to make wait for RichTextBox to complete the drawing and layout operations so that ScrollToEnd really scrolls to the end of the text?
Thanks.
Material that does not work:
EDIT : I tried the LayoutUpdated event, but it fired immediately, the same problem: the control still selects more text inside the richtextbox when it fires, even if ScrollToEnd doesn't work there ... I tried this:
richTextBox.Document.Blocks.Add(...) richTextBoxLayoutChanged = true; richTextBox.UpdateLayout(); richTextBox.ScrollToEnd();
and inside the richTextBox.LayoutUpdated event richTextBox.LayoutUpdated :
if (richTextBoxLayoutChanged) { richTextBoxLayoutChanged = false; richTextBox.ScrollToEnd(); }
The event fires correctly, but too early, richtextbox still adds more text when it ScrollToEnd , the layout is not completed, so ScrollToEnd does not work again.
EDIT 2 : Following the dowhile for the answer: MSDN on InvalidateArrange says
After the invalidation, the element will update its layout, which will occur asynchronously, unless it was forcedly called UpdateLayout.
But even
richTextBox.InvalidateArrange(); richTextBox.InvalidateMeasure(); richTextBox.UpdateLayout();
MUST NOT: after these calls, richtextbox still adds more text and sets it inside itself asynchronously. ARG!