How to determine the width of content or size a container to content
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: http://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"
IsHy开发者_JAVA技巧phenationEnabled="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>
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;
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.
精彩评论