【问题标题】:RTB now showing everyhtingRTB 现在显示一切
【发布时间】:2016-09-22 23:06:17
【问题描述】:

我有一个与 flowDocument 关联的 wpf 富文本框。 假设从 1..60 开始对流文档中的行进行编号 富文本框不够高,无法显示所有数字。

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

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

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

---添加--- 这是xml

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

--添加2---

我用

填充流文档
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

【问题讨论】:

  • 你能分享 XAML 吗?
  • 当然!请看我的补充。谢谢
  • 它滚动对我来说很好。
  • 向上滚动到第 60 行??
  • 等一下,我刚刚意识到我忘记了 FlowDocument 部分。你是如何填充文本的?

标签: c# wpf richtextbox flowdocument


【解决方案1】:

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

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

默认 XAML DWIM(实际上是“按我的意思 表示,如果我本意做错了”)布局可能是 WPF 中滚动问题的最常见原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多