I am working in C# and VS2008.
I have a WPF application containing a FlowDocument, which contains a paragraph, which contains a number of fairly short lines (i.e. Spans and LineBreaks). However the length of lines varies. I would like the FlowDocument's width to be large enough to accommodate the longest of these lines without wrapping, but no wider so as to not waste space.
Putting it into a grid column with Width=Auto doesn't work: the FlowDocument always consumes the max width allowed, since the FlowDocument can validly wrap to suit a number of different widths.
I could go through the lines as I generate them in code, and work out a width for each one, but I see no Width property on the Span or Run classes.
Any ideas on how to automatically or manually set a width for this document?
I used AndrewS's suggested idea and moved from a FlowDocument to a StackPanel of labels each containing a line. The XAML Markup is
<Border BorderThickness="2" Margin="2" BorderBrush="Black">
<ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
<StackPanel
Orientation="Vertical"
x:Name="itemsStackPanel" />
</ScrollViewer>
</Border>
The code creates spans and adds them as follows:
Label itemLabel = new Label();
// pad left and right, keep close top and bottom
itemLabel.Padding = new Thickness(2,0,2,0);
itemLabel.Content = contentSpan;
this.itemsStackPanel.Children.Add(itemLabel);
Sizing is perfect now, and the only snag is that the font doesn't look exactly the same as the FlowDocument next to it, despite the font Family and Font Size being the same (Segoe UI, 15pt), and that the Labels look much further apart than the lines in the FlowDocument. I have asked this as a separate question