WPF FlowDocument will not fill Viewbox container
I have a WPF usercontrol that I want to load with dynamic content in code using a FlowDocument: textblocks, bulleted lists, etc. Content is loaded as expected. And the box sizes looks fine in designer, but in runtime the FlowDocument insists on clipping the result and only show part of the width.
My assumption was that the Viewbox would fill the grid cell and the inner FlowDocument to fill up all available space.
I tried to set the PageWidth/PageHeigth of the FlowDocument in code but with no result.
Any help appreciated.
<Grid x:Name="grdDisplay">
<Grid.RowDefinitions>
<RowDefinition Height="22*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Viewbox x:Name="viewboxContent" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalAlignment="Left" VerticalAlignment="Top" StretchDirection="DownOnly">
<FlowDocumentScrollViewer VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden">
<FlowDocument x:Name="flowDocument">
</FlowDocument>
</FlowDocumentScrollViewer>
</Viewbox>
<Viewbox Grid.Row="1" Grid.Column="0" VerticalAlignment="Bottom" HorizontalAlignment="Left" StretchDirection="DownOnly">
<TextBlock x:Name="txtBottom" />
</Viewbox>
</Grid>
I finally got it to work!!
The problem lies in the fact that Viewbox scaling is based on the design time size of the child object. Even if the actual size of the internal child objects are bigger, they will still report their design time size also to the ActualWidth and ActualHeight properties. This is kind of confusing.
The workaround is to create the whole content including the FlowDocumentScrollViewer and its FlowDocument in runtime. They will then be based on the current available space and will fill the content as expected.
In my case the purpose was to make a simple markdown parser that will generate dynamic layout. This is now implemented and working fine :)
链接地址: http://www.djcxy.com/p/50498.html