Simple FlowDocument Rendering
Sometimes the obvious eludes us. The solution stares us in the face and we just don't see it. Recently I had such an experience.
The problem at hand was simple: I needed to show some FlowDocument contents in a fixed width and height area of my window without any scrolling – the requirement was that if there is an overflow, the contents should be cut off. The XAML is shown below:
<Window x:Class="FlowDocumentScrollViewer.Window1"
xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="200" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200" />
</Grid.ColumnDefinitions>
<FlowDocumentScrollViewer>
<FlowDocument>
<Paragraph>
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
The quick brown fox jumps over the lazy dog.
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
</Window>
Running the application will of course show the following window:

But ofcourse I don't need scrolling so I added the following attributes to the FlowDocumentScrollViewer:
VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden"
And that gives the desired effect:

But does it? If we take the mouse over the content area and use the scroll wheel on our mouse – the text starts to scroll!! Hey that was not part of the requirement!
So I started looking into the problem. What are the options on my plate?
-
Access the ScrollViewer inside the FlowDocumentScrollViewer and change a setting there so scrolling is disabled. This approach does not lead to a solution as the ScrollViewer is an internal property inside the FlowDocumentScrollViewer. That leads to the question that may be we can extract the ScrollViewer from the Visual Tree and changing the settings of the ScrollViewer there. But then the problem remains is that there is no direct property in the ScrollViewer that would disable the scrolling.
-
We can try to catch the mouse scroll event before it reaches the ScrollViewer – but again is a problem as the event is one of those bubbling event which stops at the ScrollViewer. Another failure path!
-
One last option that I could try was to change the default control template of the FlowDocumentScrollViewer to remove the ScrollViewer.
Before getting lost in option 3, I decided to post a question in the MSDN forum just in case some one could throw some light on my dilemma. I posted my question. I have to say that my question must have been very popular as it got lots of views (oops – guilty as charged – that was me constantly refreshing the thread page to see if there was a reply).
After battling for a while with the default control template, there was a finally a solution suggested on my thread. And man o man, it was the obvious and it had been staring at me the whole time and I hadn't noticed it. The solution was to have VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled". I was so thinking about a complicated solution that I forgot about the most obvious solution.
Thanks again Ben for the solution – you have saved me from getting lost in a maze. The MSDN forum thread can be found here.