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
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.
void autoresizeColumns(Table table)
{
TableColumnCollection columns = table.Columns;
TableRowCollection rows = table.RowGroups[0].Rows;
TableCellCollection cells;
TableRow row;
TableCell cell;
int columnCount = columns.Count;
int rowCount = rows.Count;
int cellCount = 0;
double[] columnWidths = new double[columnCount];
double columnWidth;
// loop through all rows
for (int r = 0; r < rowCount; r++)
{
row = rows[r];
cells = row.Cells;
cellCount = cells.Count;
// loop through all cells in the row
for (int c = 0; c < columnCount && c < cellCount; c++)
{
cell = cells[c];
columnWidth = getDesiredWidth(new TextRange(cell.ContentStart, cell.ContentEnd)) + 19;
if (columnWidth > columnWidths[c])
{
columnWidths[c] = columnWidth;
}
}
}
// set the columns width to the widest cell
for (int c = 0; c < columnCount; c++)
{
columns[c].Width = new GridLength(columnWidths[c]);
}
}
double getDesiredWidth(TextRange textRange)
{
return new FormattedText(
textRange.Text,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(
textRange.GetPropertyValue(TextElement.FontFamilyProperty) as FontFamily,
(FontStyle)textRange.GetPropertyValue(TextElement.FontStyleProperty),
(FontWeight)textRange.GetPropertyValue(TextElement.FontWeightProperty),
FontStretches.Normal),
(double)textRange.GetPropertyValue(TextElement.FontSizeProperty),
Brushes.Black,
null,
TextFormattingMode.Display).Width;
}