views:

1510

answers:

3

Hi,

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?

+4  A: 

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;
     }
  }
John Myczek
+1  A: 

Hi, i've tried this:

flowDocumentScrollViewer1.ScrollViewer.ScrollToEnd();

but i get "[ScrollViewer] is inaccessible due to its protection level"

:-(

I corrected my answer. Sorry for the confusion.
John Myczek
+3  A: 

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;
}
Anthony
Thanks for pointing out the error in my answer. I corrected it.
John Myczek
Note this FindScrollViewer method does not currently work, the version in John Myczek's answer does however.
Niall
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.
Anthony