Create Table in Flow Document in WPF



Table: The table element in WPF is for displaying information in a tabular format and according to the requirement, the table element is basically used to display elements horizontally. Whenever you want to display more than one element horizontally or we can say you want to line-up more than one element horizontally, then you can use the table element. The syntax for a table is verbose, but very logical.  You simply build it up by adding columns and groups of rows within each column.  Each row can then be split into cells and you can insert content into the individual cells. By default, the content of the FlowDocument will be added vertically.
Following are the steps which you need to follow to create a Table:

  1. Place a TableRowGroup element inside the Table. The TableRowGroup holds a group of rows, and every table consists of one or more TableRowGroup elements. On its own, the TableRowGroup doesn't do anything. However, if you use multiple groups and give them each different formatting, you get an easy way to change the overall look of your table without setting repetitive formatting properties on each row.
     

  2. Place a TableRow element inside your TableRowGroup for each row.
     
  3. Place a TableCell element inside each TableRow to represent each column in the row.
     
  4. Place a block element (typically a Paragraph) in each TableCell. This is where you'll add your content for that cell.

Example of creating a Table:

<Window x:Class="Documents.FlowContent"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="FlowContent" Height="381" Width="525" >
    <FlowDocumentScrollViewer>
        <FlowDocument>
            <Paragraph FontSize="14pt">Top 5 Gold Winner's of Common Wealth Games:</Paragraph>
            <Table>
                <TableRowGroup Paragraph.TextAlignment="left">
                    <TableRow FontWeight="Bold" >
                        <TableCell>
                            <Paragraph>Rank</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>Name</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>Medals</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>1</Paragraph>
                        </TableCell>
                        <TableCell>
                           
<Paragraph>Australia</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>74</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>2</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>India</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>38</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>3</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>England</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>37</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                       
<TableCell>
                            <Paragraph>4</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>Canada</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>26</Paragraph>
                        </TableCell>
                    </TableRow>
                    <TableRow>
                        <TableCell>
                            <Paragraph>5</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>South Africa</Paragraph>
                        </TableCell>
                        <TableCell>
                            <Paragraph>12</Paragraph>
                        </TableCell>
                    </TableRow>
                </TableRowGroup>
           
</Table>
        </FlowDocument>
    </FlowDocumentScrollViewer>
</
Window>

Output Window

table.gif

Conclusion

I hope this article helps you to understand the working of a Table in Flow Document in WPF.

Up Next
    Ebook Download
    View all
    Learn
    View all