Bent Rasmussen
Bent Rasmussen

Reputation: 5702

Scrolling a FlowDocument inside a TabControl

I have this XAML fragment:

<!-- ... -->
<TabControl>
    <TabItem>
        <!-- ... -->
    </TabItem>

    <TabItem Header="Source" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True">
        <FlowDocumentScrollViewer>
            <FlowDocument>
                <Paragraph>
                    <TextBlock
                        Text="{Binding Path=CurrentObject.Source}"
                        FontFamily="Consolas,Courier,Segoe UI"
                        FontSize="12" 
                        />
                </Paragraph>

            </FlowDocument>
        </FlowDocumentScrollViewer>
    </TabItem>
</TabControl>
<!-- ... -->

The problem is that the flow document does not scroll horizontally. I've been unable to enable that.

Any clues?

Thanks in advance.

Upvotes: 0

Views: 1072

Answers (1)

Vynos
Vynos

Reputation: 146

There are a couple things here. The first being that using a control in the Paragraph functions differently than a Run that would wrap to fit your FlowDocument.

The second is that the FlowDocument will fit your FlowDocumentScrollViewer. If you add a control to it like you did, it will fit the width of the FlowDocument and viewer and the text will go beyond the TextBlock borders. This means that your document doesn't need a scroll bar; your TextBlock would. You can see this by setting the TextWrapping property of the TextBlock to Wrap.

To get around this, set the PageWidth to something beyond the limits of the viewer width like so:

    <FlowDocumentScrollViewer>
        <FlowDocument PageHeight="1056"
                      PageWidth="816">
            <Paragraph>
                <TextBlock
                    Text="{Binding Path=CurrentObject.Source}"
                    FontFamily="Consolas,Courier,Segoe UI"
                    FontSize="12" 
                    />
            </Paragraph>

        </FlowDocument>
    </FlowDocumentScrollViewer>

or bind to your TextBlock:

    <FlowDocumentScrollViewer>
        <FlowDocument PageHeight="1056"
                      PageWidth="{Binding ElementName=Part, Path=ActualWidth}">
            <Paragraph>
                <TextBlock
                    Text="{Binding Path=CurrentObject.Source}"
                    FontFamily="Consolas,Courier,Segoe UI"
                    FontSize="12" 
                    />
            </Paragraph>

        </FlowDocument>
    </FlowDocumentScrollViewer>

The last thing is that the FlowDocumentScrollViewer has it's own HorizontalScrollBarVisibility property that you can use for this (unless some styling issue prevents it).

Upvotes: 1

Related Questions