2

EDIT:

Ok, it's kind of solved, but it feels dirty:

foreach (ContainerVisual cv in SurfaceNYTR.Helpers.VFTreeHelper.FindVisualChildren<ContainerVisual>(flowDocReader))
{
    if (cv.Parent.DependencyObjectType.SystemType.FullName == "MS.Internal.PtsHost.PageVisual")
    {
      flowDocReader.Width = cv.DescendantBounds.Width;
    }
}

I looked in Snoop, and it seems one of the ContainerVisual objects stores the correct width in its DescendantBounds property. Its parent is PageVisual (this class is internal, though, so the string compare with SystemType.FullName or GetType().ToString() was used which probably sucks)

Note: FindVisualChildren finds all children by type, source for it can be found here


My goal is to display the entire contents of a FlowDocument (that is, without paginating) in a column layout. It would have a fixed height, but the width would depend on the contents of the FlowDocument.

My problem is: FlowDocumentReader does not automatically resize to the contents of the FlowDocument. As you see in my XAML below, FlowDocumentReader.Width is 5000 units (just a large number that can accommodate most documents) -- when I make it Auto, it just clips to the width of the ScrollViewer and paginates my stuff!

Is there a proper way of solving this problem?

I also made a screenshot of what this looks like now, but the ScrollViewer scrolls past the end of the document in most cases: https://i.stack.imgur.com/3FSRl.png

<ScrollViewer x:Name="scrollViewer" 
              HorizontalScrollBarVisibility="Visible" 
              VerticalScrollBarVisibility="Disabled"
              >
        <FlowDocumentReader x:Name="flowDocReader" 
                            ClipToBounds="False" 
                            Width="5000"
                            >
            <FlowDocument   x:Name="flowDoc"
                            Foreground="#FF404040" 
                            ColumnRuleWidth="2" 
                            ColumnGap="40" 
                            ColumnRuleBrush="#FF404040" 
                            IsHyphenationEnabled="True" 
                            IsOptimalParagraphEnabled="True" 
                            ColumnWidth="150">

                <Paragraph>
                    Lorem ipsum dolor sit amet, ...etc...
                </Paragraph>
                <Paragraph>
                    Lorem ipsum dolor sit amet, ...etc...
                </Paragraph>
                <Paragraph>
                    Lorem ipsum dolor sit amet, ...etc...
                </Paragraph>
            </FlowDocument>
        </FlowDocumentReader>
</ScrollViewer>
Community
  • 1
  • 1
ISVK
  • 21
  • 3

2 Answers2

2

Could you just call Measure() on the FlowDocumentReader, passing it an infinite Size, then check the FlowDocumentReader.DesiredSize property?

flowDocumentReader.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
var desiredSize = flowDocumentReader.DesiredSize;
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Thanks very much! I tried using Measure, following your advice, but it does not report the correct size. After digging through the visual tree, I found that one of the ContainerVisual elements had the correct bounds (the one whose parent is a MS.Internal.PtsHost.PageVisual. The DescendantBounds.Width property is the correct FlowDocument width. Though... I feel a little dirty using it -- edited the original post with the code snippet if you would like to see – ISVK Jan 07 '11 at 20:12
1

I'm not clear on what you mean by "in a column layout". Do you mean multiple columns, and a horizontal scrollbar? Or do you mean a single column, and a vertical scrollbar?

If you want a single column and a vertical scrollbar, you might want to look at FlowDocumentScrollViewer instead. I've found it much easier to use than FlowDocumentReader.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • Ah, thanks -- yes, I do mean multiple columns with a horizontal scrollbar (as shown in the screenshot). I figured out a way to get the FlowDocument width, and edited the original post. Thanks! – ISVK Jan 07 '11 at 20:09