locked
FlowDocumentScrollViewer does not trigger the ScrollChanged event. RRS feed

  • Question

  • I have multiple FlowDocumentScrollViewers in different tabs. New text is added to their FlowDocuments at random intervals. The problem is that only the FlowDocumentScrollViewer in the active tab raises the ScrollChanged event when new text is added. The other viewers do not trigger the event until I switch to their tab. This is a problem in the case where I want to implement an AutoScroller like so:

             public void FDSC_ScrollChanged(object sender, ScrollChangedEventArgs e)
            {
                    if (e.VerticalOffset + e.ViewportHeight <= e.ExtentHeight - 25)
                        ... do not scroll ...
                    else
                        ... scroll to end ...
            }

    The viewers that do not call the event will eventually accumulate a large number of text lines in the background and only add them to the scroll viewer when I switch to their tab. This will not only break the AutoScroller but it will also have a performance problem that is very obvious to the user because it tries to add many lines to the viewer all at once.

    The ideal solution would be for the FlowDocumentScrollViewer to get updated/refreshed whenever something is added to its FlowDocument. Is there a way to achieve this?

    Tuesday, January 27, 2009 12:58 PM

Answers

  • I found a workaround for the auto-scrolling part

                double TotalHeight = e.VerticalOffset + e.ViewportHeight;
                double PreviousTotalHeight = e.ExtentHeight - e.ExtentHeightChange;
                if (TotalHeight >= PreviousTotalHeight - 5)                
                    ScrollToEnd();

     

    Question remains on how to avoid the performance glitch... how to update/invalidate non-visible UI elements.

    • Marked as answer by Tao Liang Friday, January 30, 2009 5:50 AM
    Tuesday, January 27, 2009 6:02 PM

All replies

  • Small parenthesis about the logic of the code above:

    In case you are wondering why I am not scrolling to the end as soon as an inline/block is added to the FlowDocument, it is because in reality I set an AutoScroll flag in the ScrollChanged event and I read that flag when I add an inline to decide whether to scroll or not. I just wanted to make the code simpler in the above example.
    Tuesday, January 27, 2009 1:29 PM
  • I found a workaround for the auto-scrolling part

                double TotalHeight = e.VerticalOffset + e.ViewportHeight;
                double PreviousTotalHeight = e.ExtentHeight - e.ExtentHeightChange;
                if (TotalHeight >= PreviousTotalHeight - 5)                
                    ScrollToEnd();

     

    Question remains on how to avoid the performance glitch... how to update/invalidate non-visible UI elements.

    • Marked as answer by Tao Liang Friday, January 30, 2009 5:50 AM
    Tuesday, January 27, 2009 6:02 PM