Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 973
ProfyTroll Avatar asked Nov 05 '22 09:11

ProfyTroll


1 Answers

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.

like image 83
ProfyTroll Avatar answered Nov 09 '22 06:11

ProfyTroll