Search code examples
c#wpfrichtextboxflowdocument

RTB now showing everyhting


I have a wpf richtextbox associated with a flowDocument. Supposing to number lines in the flowdocument from 1..60 The richtextbox is not tall enough to show all numbers. enter image description here

But that's fine since I can scroll through the mouse wheel. The problem is that this procedure doesn't scroll enough as you can see in the following picture:

enter image description here

You may see that here it merely gets to line 50 instead than to line 60. It's not a problem of overlapping with other elements.

Thank you in advance for any help. Patrick

---ADD--- Here's the xaml

    <Grid Name="grdReport_RTF" Visibility="Hidden">
        <RichTextBox x:Name="rtbReport_RTF" Margin="10"  BorderBrush="Gray" Background="White" Foreground="Black" IsEnabled="True" Padding="10" Style="{DynamicResource rtbStyleDocLocal}" />
    </Grid>

with

<Style x:Key="rtbStyleDocLocal" TargetType="{x:Type RichTextBox}">
        <Style.Resources>
            <Style x:Key="{x:Type FlowDocument}" TargetType="{x:Type FlowDocument}">
                <Setter Property="OverridesDefaultStyle" Value="true"/>
            </Style>
            <Style x:Key="{x:Type Hyperlink}" BasedOn="{StaticResource {x:Type Hyperlink}}" TargetType="{x:Type Hyperlink}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="Blue"/>
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="true">
                        <Setter Property="Foreground" Value="Black"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Style.Resources>
        <Setter Property="MinWidth" Value="10"/>
        <Style.BasedOn>
            <StaticResource ResourceKey="{x:Type TextBoxBase}"/>
        </Style.BasedOn>
    </Style>

--ADD2---

I populate the flowdoc with

Application.Current.Dispatcher.Invoke(new Action(() => fdocRTB.Blocks.Clear()));
if (File.Exists(strCompleteFilename))
{
    using (var sr = new StreamReader(strCompleteFilename))
    {
        String line = sr.ReadToEnd();
        Application.Current.Dispatcher.Invoke((Action)(() => fdocRTB = AddLineToRtb(fdocRTB_Beretta, line, false, RTB_LINEHEIGHT, RTB_FONTSIZE)));
    }
}

and with that

    private FlowDocument AddLineToRtb(FlowDocument fdoc, string str, bool IsTitle, double lineHeight, double fontSize)
    {
        Paragraph par;
        var run = new Run(str);
        run.Foreground = Brushes.Black;
        if (!IsTitle)
            run.FontWeight = FontWeights.Normal;
        else
            run.FontWeight = FontWeights.Black;
        run.FontSize = fontSize;
        run.FontFamily = ffRtb;
        par = new Paragraph() { LineHeight = lineHeight, FontSize = fontSize };
        par.Inlines.Add(run);
        Application.Current.Dispatcher.Invoke((Action)(() => fdoc.Blocks.Add(par)));
        return fdoc;
    }

--Add 3-- Some more clues to the solution: if the read file contains numbers 1..60 by scrolling down to the max the effect is:

[![enter code here][3]][3]

instead if I add some more lines with letter a...z by scrolling down to the max the effect is:

enter image description here

so well beyond 60!!!! But not to z


Solution

  • Have you confirmed that it's not stretching the RTB outside the visible area? Easiest way to check that is just set Height="200" on the RTB and see how the scrolling works.

    In fact, in your screenshots, I think I do see a 1px lighter gray border down the side, but not across the bottom -- that would suggest that the actual bottom of the control has been pushed down out of sight. The effect will be easier to be sure of if you give it BorderBrush="Red", just for testing.

    Default XAML DWIM (actually "do what I would mean, if I meant to do it wrong") layout is probably the most common cause of scrolling issues in WPF.