I'm working on a chat client using the .NET Framework 3.5 and WPF. I use a TabControl to handle multiple chat windows. Inside each tab, I use a FlowDocumentScrollViewer to display the chat log. When content is added to the chat log, I call ScrollToBottom() on the ScrollViewer (unless the user has moved the scroll bar away from the bottom).
bool shouldScroll = true;
if (ScrollViewer.VerticalOffset < ScrollViewer.ExtentHeight - ScrollViewer.ViewportHeight)
shouldScroll = false;
chatLogDocument.Blocks.Add(myNewText);
if (shouldScroll)
ScrollViewer.ScrollToBottom();
The problem is, the chat log does not scroll unless the tab is active. The call to ScrollToBottom() seems to modify the ScrollViewer's VerticalOffset, but when I switch to the tab, the scroll bar will not be positioned at that location -- it's left right where it was when the tab was last active.
I've tried using ScrollToVerticalOffset() inside of the Loaded event and manually calling UpdateLayout(), but that doesn't appear to work.
I'm assuming the visual tree is collapsed for content inside a tab that is inactive, which might explain why the scroll bar isn't moved at that time, but I'm baffled that it can be left out of sync with the VerticalOffset value.
Any insight or possible workarounds?