C# 如何使WPF窗口的大小适合流文档表
我想在自己的WPF窗口中显示流文档表的一些内容。窗口的大小应确保所有表格内容都可以显示,但不能再大 当我将窗口的SizeToContent属性设置为“WidthAndHeight”时,窗口的高度与表格的高度完全相同,但窗口的宽度会拉伸到与显示器的大小一样大。窗口宽度与表的宽度相同,但表被拉伸以匹配窗口,并且所有表列都太宽: 此外,所有列的宽度都相同,即使我将每列的宽度设置为GridLength.Auto。这没有帮助,因为当我缩小窗口时,一些列开始换行,而其他列的大小仍然过大。目标是每个列只使用实际需要的空间 我需要实现相反的效果:表列的宽度应与其中的文本一样宽,窗口的大小应与所有列的总和一样大 这是我的密码:C# 如何使WPF窗口的大小适合流文档表,c#,wpf,xaml,flowdocument,C#,Wpf,Xaml,Flowdocument,我想在自己的WPF窗口中显示流文档表的一些内容。窗口的大小应确保所有表格内容都可以显示,但不能再大 当我将窗口的SizeToContent属性设置为“WidthAndHeight”时,窗口的高度与表格的高度完全相同,但窗口的宽度会拉伸到与显示器的大小一样大。窗口宽度与表的宽度相同,但表被拉伸以匹配窗口,并且所有表列都太宽: 此外,所有列的宽度都相同,即使我将每列的宽度设置为GridLength.Auto。这没有帮助,因为当我缩小窗口时,一些列开始换行,而其他列的大小仍然过大。目标是每个列只使用
<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>
问题
请使用流文档提出代码隐藏解决方案(不是XAML,因为表内容是在运行时创建的),其中列只占用其内容所需的空间,窗口仅与表一样宽
对于那些无法抗拒将问题标记为重复问题的人
stackoverflow上有一些人在简要阅读问题后将问题标记为重复问题,以防止问题被正确讨论。这相当令人沮丧。在这样做之前,请仔细考虑其他解决方案是否仅仅是关于表格?这个问题是关于将窗口宽度与表宽度匹配的,应该提供完整的代码 如果还可以传递要向其中添加特定单元格的列的索引,则此解决方案可能是可行的
//Global
long[] sizes = new long[3]{0,0,0};
//Function Call with column index as second parameter
addCell(firstRow,0, "First top line left", topLeft);
//Function
private void addCell(TableRow tableRow,int column, string text, int position)
{
Size measure = TextRenderer.MeasureText(text, SystemFonts.DefaultFont);
if (measure.Width > sizes[column])
{
sizes[column] = measure.Width;
mainTable.Columns[column].Width = new System.Windows.GridLength(measure.Width);
}
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);
}
}
您可能必须编辑MeasureText()函数才能使用实际使用的字体。这里有一个更详细的代码,明确显示了测量和设置列宽:。但我不喜欢将列宽设置为固定值,因为这会破坏其他功能。不管怎样,我将表格列宽设置为“自动”。在WPF的其他地方,这意味着大小应该根据内容而定。我做错什么了吗?如何正确使用汽车?
//Global
long[] sizes = new long[3]{0,0,0};
//Function Call with column index as second parameter
addCell(firstRow,0, "First top line left", topLeft);
//Function
private void addCell(TableRow tableRow,int column, string text, int position)
{
Size measure = TextRenderer.MeasureText(text, SystemFonts.DefaultFont);
if (measure.Width > sizes[column])
{
sizes[column] = measure.Width;
mainTable.Columns[column].Width = new System.Windows.GridLength(measure.Width);
}
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);
}
}