Scroll a WPF FlowDocumentScrollViewer from code?

10,227

Solution 1

The other answers given here are a bit puzzling, since I don't see any public "ScrollViewer" property on the FlowDocumentScrollViewer.

I hacked around the problem like this. Beware that this method can return null during initialization:

public static ScrollViewer FindScrollViewer(this 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;
}

Solution 2

try:

Scroller.ScrollViewer.ScrollToEnd();

Where "Scroller" is the name of your FlowDocumentScrollViewer.

EDIT: I wrote this answer a little too quickly. FlowDocumentScrollViewer does not expose a ScrollViewer property. I had actually extended the FlowDocumentScrollViewer class and implemented the ScrollViewer property myself. Here is the implementation:

  /// <summary>
  /// Backing store for the <see cref="ScrollViewer"/> property.
  /// </summary>
  private ScrollViewer scrollViewer;

  /// <summary>
  /// Gets the scroll viewer contained within the FlowDocumentScrollViewer control
  /// </summary>
  public ScrollViewer ScrollViewer
  {
     get
     {
        if (this.scrollViewer == null)
        {
           DependencyObject obj = this;

           do
           {
              if (VisualTreeHelper.GetChildrenCount(obj) > 0)
                 obj = VisualTreeHelper.GetChild(obj as Visual, 0);
              else
                 return null;
           }
           while (!(obj is ScrollViewer));

           this.scrollViewer = obj as ScrollViewer;
        }

        return this.scrollViewer;
     }
  }

Solution 3

I've faced a similar problem: I wanted a textual area which could hold my text, is able to wrap it, it fills its parent control and is scrollable.

First I've tried to use a TextBlock with a ScrollViewer and I think it worked, but for some reason I've wanted to use a FlowDocument instead with a FlowDocumentScrollViewer. This latter didn't work and I just couldn't leave the fight unattented so I tried to find solutions and this is how I got here. I've tried to apply the workarounds presented in the answers to the original question, however neither solutions worked out for me (I'm using .NET 4.5, maybe it works in other versions, but I don't know about that).

I've tried using a single FlowDocument by itself also, but the control contains some UI elements I didn't want. So, I came up with another solution.

  <ScrollViewer VerticalScrollBarVisibility="Auto">
    <FlowDocumentScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden">
      <FlowDocument>

That's right. It works! Calling ScrollViewer.ScrollToBottom() just works! The ScrollViewer enables scrolling and FlowDocumentScrollViewer removes the UI elements from the FlowDocument. Hope it helps!


Apparently my construction had a flaw, because this way the FlowDocument isn't scrollable via a mouse's scrolling wheel. However setting the FlowDocumentScrollViewer control's IsHitTestVisible property to False solves this.

Solution 4

This question was asked 7 years ago, now I have the same problem, and I find a simple solution. The follow code add a Section to Flowdocument which same to Paragraph, then scroll to the end.

private void addSection(Section section)
{
    section.Loaded += section_Loaded;
    fdoc.Blocks.Add(section);
}

private void section_Loaded(object sender, RoutedEventArgs e)//scroll to end
{
    var sec = sender as Section;
    if (sec != null)
    {
        sec.BringIntoView();
    }
}

Solution 5

This may be a very late answer, but I've found a way to do this.

//after your FlowDocumentScrollViewer(for example, x:Name="fdsv") loaded
ScrollViewer sv = fdsv.Template.FindName("PART_ContentHost", fdsv) as ScrollViewer;

sv.ScrollToBottom();
sv.ScrollToTop();
sv.ScrollToVerticalOffset(100);
// etc.

Check IScrollInfo and ScrollViewer for details.

I hope this helps you.

Share:
10,227
jmistx
Author by

jmistx

Utterly Insane developer

Updated on June 16, 2022

Comments

  • jmistx
    jmistx almost 2 years

    I have a FlowDocumentScrollViewer I want to automatically scroll to the bottom when text is added.

    <FlowDocumentScrollViewer Name="Scroller">
     <FlowDocument Foreground="White" Name="docDebug" FontFamily="Terminal">
      <Paragraph Name="paragraphDebug"/>
     </FlowDocument>
    </FlowDocumentScrollViewer>
    

    In code I add Inlines to the Paragraph, but when there is to much text I would like to be able to simply scroll down using code instead of having the user doing so.

    Any suggestions?

  • turbosaurus_rex
    turbosaurus_rex about 15 years
    Thanks for pointing out the error in my answer. I corrected it.
  • Niall
    Niall almost 14 years
    Note this FindScrollViewer method does not currently work, the version in John Myczek's answer does however.
  • Anthony
    Anthony almost 14 years
    By "currently", do you mean in WPF 4.0? Looking at it again, it could be fragile and broken by changes to the exact controls that lie between the FlowDocumentScrollViewer and the ScrollViewer caused by version or styling, so a simpler and more ignorant approach may be better.
  • paparazzo
    paparazzo about 11 years
    I get and error this cannot be converted to a DependencyObject
  • Norbert Hüthmayr
    Norbert Hüthmayr over 3 years
    Beware of the IsHitTestVisible - it will disable selecting text in your FlowDocumentScrollViewer