I want a table to logically size the columns according to the contents. Is this possible in WPF?
alt text http://img43.imageshack.us/img43/2640/flowdocument.jpg
Here is the code I'm working with:
<Window x:Class="FlowDocument.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style TargetType="{x:Type TableCell}">
<Setter Property="BorderBrush" Value="Gray" />
<Setter Property="BorderThickness" Value="3" />
</Style>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Padding" Value="2, 2, 2, 2" />
</Style>
</Window.Resources>
<Grid>
<FlowDocumentScrollViewer>
<FlowDocument>
<Table>
<Table.Columns>
<TableColumn Background="LightBlue" />
<TableColumn Background="Coral" />
</Table.Columns>
<TableRowGroup>
<TableRow>
<TableCell>
<Paragraph>This is a long piece of text</Paragraph>
</TableCell>
<TableCell>
<Paragraph>This isn't</Paragraph>
</TableCell>
</TableRow>
<TableRow>
<TableCell>
<Paragraph>This is a another long piece of text. The column should be wider than the other one!</Paragraph>
</TableCell>
<TableCell>
<Paragraph>Ditto</Paragraph>
</TableCell>
</TableRow>
</TableRowGroup>
</Table>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
</Window>
It is possible by determining the desired width of the widest cell of a column. The widest cell can be determined by looping through all the rows determining the desired width of the cell and remembering the biggest value.
In this example, all columns are optimized. The value of 19 might result from the left and right cell padding plus cell border thickness.
it's not quite what you're looking for, but you can do something like