首页 > 解决方案 > FlowDocument 的网格替代方案?

问题描述

根据我的阅读,不能在 FlowDocument 中使用 Grid。

 <TextBlock Margin="0 10" FontFamily="Ubuntu"  FontSize="19" FontWeight="DemiBold" Text="Account details"></TextBlock>
 <Separator  Width="600"></Separator>
 <StackPanel Margin="0 10" Orientation="Horizontal">

       <TextBlock Margin="0 0 30 0"  Text="Account ID"></TextBlock>
       <TextBlock Margin="0 0 30 0" Text="Username"></TextBlock>
       <TextBlock Margin="0 0 30 0"  Text="Password"></TextBlock>
       <TextBlock Margin="0 0 30 0"  Text="Creation date"></TextBlock>

    </StackPanel>
 <Separator  Width="600"></Separator>
  <StackPanel Margin="0 10" Orientation="Horizontal">

    <TextBlock  Width="102" Margin="0 0 30 0"  ></TextBlock>
    <TextBlock  Width="Auto" Margin="0 0 30 0" ></TextBlock>
    <TextBlock   Width="Auto" Margin="0 0 30 0"  ></TextBlock>
    <TextBlock   Width="91" Margin="0 0 30 0"  ></TextBlock>

  </StackPanel>

 </StackPanel>

输出:

在此处输入图像描述

现在的问题是,如果我填写Textblock's第二个 StackPanel,如果它们很长,它们将不会显示所有文本,这是预期的,因为我已经为它们设置了特定的宽度。如果我将其设置为Auto它将显示,但每个内容都Textblock不会与列对齐,并且会更混乱。

我目前的选择是什么?

标签: c#wpf

解决方案


FlowDocument如果你想在你的(即你想画一个表格)中创建一种网格,你可以使用Table类。它派生自Block元素,因此可以包含在FlowDocument.

Table很容易使用:

<FlowDocument>
  <Table>
    <!-- 
      This table has 3 columns, each described by a TableColumn 
      element nested in a Table.Columns collection element. 
    -->
    <Table.Columns>
      <TableColumn />
      <TableColumn />
      <TableColumn />
    </Table.Columns>
    <!-- 
      This table includes a single TableRowGroup which hosts 2 rows,
      each described by a TableRow element.
    -->
    <TableRowGroup>
      <!--
        Each of the 2 TableRow elements hosts 3 cells, described by
        TableCell elements.
      -->
      <TableRow>
        <TableCell>
          <!-- 
            TableCell elements may only host elements derived from Block.
            In this example, Paragaph elements serve as the ultimate content
            containers for the cells in this table.
          -->
          <Paragraph>Cell at Row 1 Column 1</Paragraph>
        </TableCell>
        <TableCell>
          <Paragraph>Cell at Row 1 Column 2</Paragraph>
        </TableCell>
        <TableCell>
          <Paragraph>Cell at Row 1 Column 3</Paragraph>
        </TableCell>
      </TableRow>
      <TableRow>
        <TableCell>
          <Paragraph>Cell at Row 2 Column 1</Paragraph>
        </TableCell>
        <TableCell>
          <Paragraph>Cell at Row 2 Column 2</Paragraph>
        </TableCell>
        <TableCell>
          <Paragraph>Cell at Row 2 Column 3</Paragraph>
        </TableCell>
      </TableRow>
    </TableRowGroup>
  </Table>
</FlowDocument>

你可以在这里找到更多信息。我希望它可以帮助你。


推荐阅读