FlowDocumentReader scroll to top

432 views Asked by At

How to scroll to the top of a FlowDocumentReader?

Content is set by binding

<FlowDocumentReader Grid.Row="4" Grid.Column="0" Name="FlowDocumentPageViewer1" HorizontalAlignment="Stretch">
    <FlowDocumentReader.Document>
        <Binding ElementName="_this" Path="DocFlow" IsAsync="False" Mode="OneWay"/>
    </FlowDocumentReader.Document>                   
</FlowDocumentReader>

If I scroll down then bind new content it does not scroll to the top.
With new content I want to scroll to the top.

Based on comment from Clemnes this scrolls to the top

FlowDocumentPageViewer1.Document.BringIntoView();

Now my problem is how to automate that call.
I cannot put it in the get as cannot put that command after the return.
Tried these two events but are not fired with a binding update

Loaded="FlowDocumentPageViewer1_loaded"  
SourceUpdated="FlowDocumentPageViewer1_loaded"
1

There are 1 answers

1
Clemens On

You may create an attached property that sets the original Document property and also calls BringIntoView():

public class FlowDocumentReaderEx
{
    public static readonly DependencyProperty DocumentProperty =
        DependencyProperty.RegisterAttached(
            "Document", typeof(FlowDocument), typeof(FlowDocumentReaderEx),
            new FrameworkPropertyMetadata(DocumentPropertyChanged));

    public static FlowDocument GetDocument(DependencyObject obj)
    {
        return (FlowDocument)obj.GetValue(DocumentProperty);
    }

    public static void SetDocument(DependencyObject obj, FlowDocument value)
    {
        obj.SetValue(DocumentProperty, value);
    }

    private static void DocumentPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var flowDocumentReader = obj as FlowDocumentReader;

        if (flowDocumentReader != null)
        {
            flowDocumentReader.Document = e.NewValue as FlowDocument;

            if (flowDocumentReader.Document != null)
            {
                flowDocumentReader.Document.BringIntoView();
            }
        }
    }
}

Now you can bind this property like:

<FlowDocumentReader ...
    local:FlowDocumentReaderEx.Document="{Binding DocFlow, ElementName=_this}"/>