RichTextBox vs FlowDocumentScrollViewer - Why do they look so different?

2.2k views Asked by At

I have a very simple xaml file where I am passing the same Paragraph and Run elements to both a RichTextBox and a FlowDocumentScrollViewer. The both look radically different - which is not what I was expecting.

I understand that you can style either the FlowDocument or the containers so they look the same but I was expecting them both to inherit the same 'default' settings.

Here is my code:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="80" />
        <RowDefinition Height="80" />
        <RowDefinition Height="80" />
    </Grid.RowDefinitions>
    <RichTextBox Grid.Row="0">
        <FlowDocument>
            <Paragraph>
                <Run>Here is some text</Run>
                <LineBreak />
                <Run>Here is some more text</Run>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
    <TextBlock Grid.Row="1" Padding="6,0,0,0">
        <Run>Here is some text</Run>
        <LineBreak />
        <Run>Here is some more text</Run>
    </TextBlock>
    <FlowDocumentScrollViewer Grid.Row="2" IsHitTestVisible="True" VerticalScrollBarVisibility="Hidden">
        <FlowDocument>
            <Paragraph>
                <Run>Here is some text</Run>
                <LineBreak />
                <Run>Here is some more text</Run>
            </Paragraph>
        </FlowDocument>
    </FlowDocumentScrollViewer>
</Grid>

My Question

Is there some way of ensuring that both RichTextBox and the FlowDocumentScrollViewer display the FlowDocument in the same way? Ideally so you can't tell the difference between them - without having to 'hard code' margins, fonts etc into one or the other.

You'll notice in my example above my Textblock requires some Margin to get it to display the same as the RichTextBlock, but I really want to avoid having to do anything like this as there will no doubt be a situation where some font or culture setting breaks this all horribly.

1

There are 1 answers

0
Minustar On

I am no expert in WPF, especially since I really use the RichTextBox but binding the properties of either one onto the other with styles (perhaps even templating) can perhaps solve your problem.

The default properties of FlowDocument differ from the ones of RTB or TB. (The default font for FlowDocument being Georgia!!!)

    <RichTextBox>
        <FlowDocument Name="rtDoc"
                      PagePadding="{Binding PagePadding, ElementName=flDoc}"
                      ...
                      FontFamily="{Binding FontFamily, ElementName=flDoc}">
            ...
        </FlowDocument>
    </RichTextBox>
        ...
    <FlowDocumentScrollViewer>
        <FlowDocument Name="flDoc" />
    </FlowDocumentScrollViewer>

Hope you can use this!