4

A normal ScrollViewer appears to have a method that accomplishes this. A FlowDocumentScrollViewer does not. So how do I go around setting a FlowDocumentScrollViewer to the bottom.

PPFY
  • 67
  • 6

2 Answers2

3

Or you can use the provided answer here, and search for the ScrollViewer:

public static void ScrollToEnd(FlowDocumentScrollViewer fdsv)
{
    ScrollViewer sc = FindVisualChildren<ScrollViewer>(fdsv).First() as ScrollViewer;
    if (sc != null)
        sc.ScrollToEnd();
}


public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}
Community
  • 1
  • 1
rmojab63
  • 3,513
  • 1
  • 15
  • 28
2

You can access scrollviewer from flowdocumentscrollviewer. Here is example

 private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var scrollViewer = FindScrollViewer(flowDocumentScrollViewer);
        scrollViewer.ScrollToBottom();
    }

    public static ScrollViewer FindScrollViewer(FlowDocumentScrollViewer flowDocumentScrollViewer)
    {
        if (VisualTreeHelper.GetChildrenCount(flowDocumentScrollViewer) == 0)
        {
            return null;
        }

        // Border is the first child of first child of a ScrolldocumentViewer
        DependencyObject firstChild = VisualTreeHelper.GetChild(flowDocumentScrollViewer, 0);
        if (firstChild == null)
        {
            return null;
        }

        Decorator border = VisualTreeHelper.GetChild(firstChild, 0) as Decorator;

        if (border == null)
        {
            return null;
        }

        return border.Child as ScrollViewer;
    }
Shashi Bhushan
  • 1,292
  • 11
  • 15
  • 1
    Well, that certainly works. Is it standard practice to just do this, or do you generally not want to scroll to the bottom of a flowdocumentscrollviewer? – PPFY Feb 11 '17 at 07:26
  • It is kind of utility method for function which is not exposed. I don't see any problem in using this. – Shashi Bhushan Feb 11 '17 at 07:50