如何使WPF窗口的大小适合流文档表

时间:2017-06-30 15:31:21

标签: c# wpf xaml flowdocument

我想在自己的WPF窗口中显示流文档表的一些内容。窗口应该只有大,以便可以显示所有表格内容,但不能更大。

当我将窗口的SizeToContent属性设置为" WidthAndHeight"时,窗口的高度与表格正确相同,但窗口宽度会拉伸到我的大小。监控。窗口宽度与表格的宽度相同,但是表格被拉伸以匹配窗口,并且所有表格列都太宽:

Table

此外,所有列都具有相同的宽度,即使我将每列的宽度设置为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>

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);
    }
  }
}

问题

请使用流文档提出解决方案背后的代码(不是XAML,因为表内容是在运行时创建的),其中列只占用其内容所需的空间,窗口的宽度仅与表一样宽。< / p>

对那些无法将问题标记为重复的人

有些人在stackoverflow上通过简单地阅读问题后将这些问题标记为重复,从而阻止了问题的正确讨论。这是相当令人沮丧的。在您这样做之前,请仔细考虑其他解决方案是否与表格有关?这个问题明确是关于将窗口宽度与表格宽度相匹配,并且应该提供完整的代码。

1 个答案:

答案 0 :(得分:1)

如果您还可以传递要添加特定单元格的列的索引,则此解决方案可能是可行的。

//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 ()函数才能使用您实际使用的字体。