RTB 现在显示一切

RTB now showing everyhting

我有一个与 flowDocument 关联的 wpf richtextbox。 假设流文档中的行号从 1..60 richtextbox 不够高,无法显示所有数字。

但这很好,因为我可以滚动鼠标滚轮。 问题是这个过程滚动不够,如下图所示:

你可能会看到这里它只是到达第 50 行而不是第 60 行。 不是和其他元素重叠的问题

提前感谢您的帮助。 帕特里克

---添加--- 这是 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>

<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---

我用

填充 flowdoc
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)));
    }
}

然后

    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;
    }

--加3-- 解决方案的更多线索: 如果通过向下滚动到最大值读取的文件包含数字 1..60,则效果为:

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

相反,如果我通过向下滚动到最大值来添加更多带有字母 a...z 的行,效果是:

超过 60 岁了!!!!但不是 z

您确认它没有将 RTB 拉伸到可见区域之外吗?最简单的检查方法是在 RTB 上设置 Height="200" 并查看滚动的工作原理。

事实上,在您的屏幕截图中,我想我确实在侧面看到了一个 1px 的浅灰色边框,但没有穿过底部 -- 这表明控件的实际底部已被向下推到看不见的地方.如果给BorderBrush="Red",效果更容易确定,只是为了测试。

默认 XAML DWIM(实际上是 "do what I would mean, if I meant to do it wrong")布局可能是 WPF 中滚动问题的最常见原因。