在WPF中展示大量文本的最佳方法是什么?

5

我需要在 WPF 代码中展示大量文本数据。起初,我尝试使用 TextBox(当然,渲染速度太慢了)。现在我正在使用 FlowDocument,它很棒,但最近又有一个要求:文本不应该被连字号分隔。据说这是不可能的(document.IsHyphenationEnabled = false),但我仍然看不到我的宝贵水平滚动条。如果我放大文本比例,就会出现连字号。

alt text

public string TextToShow
{
    set
    {
        Paragraph paragraph = new Paragraph();
        paragraph.Inlines.Add(value);

        FlowDocument document = new FlowDocument(paragraph);
        document.IsHyphenationEnabled = false;

        flowReader.Document = document;
        flowReader.IsScrollViewEnabled = true;
        flowReader.ViewingMode = FlowDocumentReaderViewingMode.Scroll;
        flowReader.IsPrintEnabled = true;
        flowReader.IsPageViewEnabled = false;
        flowReader.IsTwoPageViewEnabled = false;
    }
}

这就是我创建FlowDocument的方式 - 这里是我WPF控件的一部分:
<FlowDocumentReader Name="flowReader" Margin="2 2 2 2" Grid.Row="0" />

没有犯罪行为 =))

我想知道如何驯服这个野兽 - 搜索没有什么有用的信息。或者您有其他的方法来显示大量文本,或者文本框有一些虚拟化功能,我只需要启用即可。无论如何,我很高兴听到您的回复!


2
你的问题不是连字号问题,而是自动换行问题。看一下这里:http://msdn.itags.org/visual-studio/36912/,他们建议将段落宽度设置为大于视窗宽度。 - OmerGertel
谢谢Omer - 链接和建议似乎非常合理。很快会尝试 :) - ProfyTroll
请查看此处的答案:https://dev59.com/xEfRa4cB1Zd3GeqP82cQ#46546877 - juFo
1个回答

2

这实际上是换行而不是连字。可以通过将FlowDocument.PageWidth设置为合理的值来克服这个问题,唯一的问题是如何确定此值。

Omer提出了这个方法msdn.itags.org/visual-studio/36912/,但我不喜欢使用TextBlock作为文本测量工具。更好的方法:

            Paragraph paragraph = new Paragraph();
            paragraph.Inlines.Add(value);


            FormattedText text = new FormattedText(value, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(paragraph.FontFamily, paragraph.FontStyle, paragraph.FontWeight, paragraph.FontStretch), paragraph.FontSize, Brushes.Black );

            FlowDocument document = new FlowDocument(paragraph);
            document.PageWidth = text.Width*1.5;
            document.IsHyphenationEnabled = false;

Omer - 感谢您的指导。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接