I'd like to display some content of a flow document table in its own WPF Window. The Window should only be as big so that all table content can be displayed, but not any bigger.
When I set the window's SizeToContent property to "WidthAndHeight", the height of the window is correctly the same as the table, but the window width gets stretched as big as the size of my monitor. The window width is the same width as the table, but the table got stretched to match the window and all table columns are too wide:
Furthermore, all columns have the same width, even I set each column's Width to GridLength.Auto. This is not helpful, because when I make the window smaller, some columns start to word wrap, while other columns still have too much size. The goal is that each column uses only as much space as really needed.
I need to achieve the opposite: The table columns should only be as wide as the text in it and the window should only be as big as all columns together.
Here is my code:
<Window x:Class="TryFlowTable.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" SizeToContent="WidthAndHeight">
<FlowDocumentScrollViewer Name="FlowDocumentViewer"/>
</Window>
C#
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace TryFlowTable {
public partial class MainWindow: Window {
public MainWindow() {
InitializeComponent();
var flowDocument = new FlowDocument();
flowDocument.PagePadding = new Thickness(0);
FlowDocumentViewer.Document = flowDocument;
var mainTable = new Table();
mainTable.CellSpacing = 0;
flowDocument.Blocks.Add(mainTable);
mainTable.Columns.Add(new TableColumn { Width=GridLength.Auto});
mainTable.Columns.Add(new TableColumn { Width=GridLength.Auto });
mainTable.Columns.Add(new TableColumn { Width=GridLength.Auto });
var mainTableRowGroup = new TableRowGroup();
mainTable.RowGroups.Add(mainTableRowGroup);
var firstRow = new TableRow();
mainTableRowGroup.Rows.Add(firstRow);
addCell(firstRow, "First top line left", topLeft);
addCell(firstRow, "Top middle", topRight);
addCell(firstRow, "First top line right", topRight);
var middleRow = new TableRow();
mainTableRowGroup.Rows.Add(middleRow);
addCell(middleRow, "Left", left);
addCell(middleRow, "Middle", right);
addCell(middleRow, "Right", right);
}
const int topLeft = 1;
const int topRight = 2;
const int left = 3;
const int right = 4;
private void addCell(TableRow tableRow, string text, int position) {
var run = new Run(text);
var tableCell = new TableCell(new Paragraph(run));
tableCell.BorderBrush = Brushes.Gray;
if (position == topLeft) {
tableCell.BorderThickness = new Thickness(1, 1, 1, 1);
} else if (position == topRight) {
tableCell.BorderThickness = new Thickness(0, 1, 1, 1);
} else if (position == left) {
tableCell.BorderThickness = new Thickness(1, 0, 1, 1);
} else if (position == right) {
tableCell.BorderThickness = new Thickness(0, 0, 1, 1);
}
tableRow.Cells.Add(tableCell);
}
}
}
Question
Please propose a code behind solution (not XAML, because the table content gets created during runtime) using a flow document, where columns take only the space needed by their content and where the window is only as wide as the table.
To those who can't resist to mark questions as duplicates
There are some people on stackoverflow who prevent that questions can be properly discussed, by marking them as duplicates after reading the question briefly. This is rather frustrating. Before you do so, please consider carefully if the other solution is just about tables ? This question is explicitly about matching the window width to the table width and the complete code behind should be provided.