ProfyTroll
ProfyTroll

Reputation: 391

Best way to show huge text in WPF?

I need to show a really huge amount of text data in WPF code. First i tried to use TextBox (and of course it was too slow in rendering). Now i'm using FlowDocument--and its awesome--but recently i have had another request: text shouldnt be hyphenated. Supposedly it is not (document.IsHyphenationEnabled = false) but i still don't see my precious horizontal scroll bar. if i magnify scale text is ... hyphenated.

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

That's how i create FlowDocument - and here comes part of my WPF control:

<FlowDocumentReader Name="flowReader" Margin="2 2 2 2" Grid.Row="0" />

Nothing criminal =))

I'd like to know how to tame this beast - googled nothing helpful. Or you have some alternative way to show megabytes of text, or textbox have some virtualization features which i need just to enable. Anyway i'll be happy to hear your response!

Upvotes: 5

Views: 4640

Answers (1)

ProfyTroll
ProfyTroll

Reputation: 391

It's really wrapping not hyphenation. And one can overcome this by setting FlowDocument.PageWidth to reasonable value, the only question was how to determine this value. Omer suggested this recipe msdn.itags.org/visual-studio/36912/ but i dont like using TextBlock as an measuring instrument for text. Much better way:

            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 - thanks for the direction.

Upvotes: 2

Related Questions