C# WPF Navigate to FlowDocument in a Frame: Need to set ViewMode=Scroll

342 Views Asked by At

When navigating to a FlowDocument in a Frame, the FlowDocumentReader defaults to ViewMode=Page. I need to get a reference to the FlowDocumentReader so that I can set the ViewMode property to Scroll.

I can get a reference to the FlowDocument object by casting the Frame's Content property to a FlowDocument, but I cannot find a reference to the FlowDocumentReader that is instantiated when I navigate to the document.

I understand that the user can easily click on the scroll view button in the FlowDocumentReader, but I should be able to do this programmatically.

1

There are 1 best solutions below

0
On

I was barking up the wrong tree, literally! The answer to my question was that the FlowDocumentReader is part of the Visual tree. I had to go hunting for it. There are probably more elegant ways to do this, but this one worked:

    static public void SetReaderModeToScroll(Visual myVisual)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
        {
            // fetch the child
            Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

            // attempt to cast it to a FlowDocumentReader
            try
            {
                FlowDocumentReader reader = (FlowDocumentReader) childVisual;

                // if we get this far, we've found the reader
                reader.ViewingMode = FlowDocumentReaderViewingMode.Scroll;
                return;
            }
            // catch the exception if it doesn't work
            catch (Exception e)
            {
            }
            // Drill down another level and keep looking
            SetReaderModeToScroll(childVisual);
        }
    }