Creating a simple Table in WPF?

2019-03-19 23:30发布

I was wondering if there is a way (any components/controls) that allow me to draw a simple Microsoft Word style table in my application window. Something like this:

Sample Table

Any ideas?

2条回答
我只想做你的唯一
2楼-- · 2019-03-19 23:51

I would recommend starting with WPF Toolkit DataGrid control.

Here is an ok tutorial on how to use it: http://www.switchonthecode.com/tutorials/using-the-wpf-toolkit-datagrid

查看更多
beautiful°
3楼-- · 2019-03-19 23:54

It depends on how you want to use it. Either use one of the ItemsControl (like DataGrid, ListView etc), do it directly with a Grid panel (as recommended by the other answers) or use a FlowDocument

FlowDocument allows you to specify Tables, Rows and Columns. You can also select several cells at once for Copy/Paste etc.

enter image description here

<FlowDocumentReader UseLayoutRounding="True" SnapsToDevicePixels="True">
    <FlowDocumentReader.Resources>
        <Style TargetType="TableCell">
            <Setter Property="TextAlignment" Value="Center"/>
        </Style>
    </FlowDocumentReader.Resources>
    <FlowDocument>
        <Table CellSpacing="0">
            <Table.Columns>
                <TableColumn/>
                <TableColumn/>
                <TableColumn/>
                <TableColumn/>
            </Table.Columns>
            <TableRowGroup>
                <TableRow>
                    <TableCell BorderBrush="Black" BorderThickness="1">
                        <Paragraph FontWeight="Bold">Category</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,1,1,1">
                        <Paragraph FontWeight="Bold">A</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,1,1,1">
                        <Paragraph FontWeight="Bold">B</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,1,1,1">
                        <Paragraph FontWeight="Bold">C</Paragraph>
                    </TableCell>
                </TableRow>
                <TableRow>
                    <TableCell BorderBrush="Black" BorderThickness="1,0,1,1">
                        <Paragraph FontWeight="Bold">Subscription</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>Monthly</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>Yearly</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>Monthly</Paragraph>
                    </TableCell>
                </TableRow>
                <TableRow>
                    <TableCell BorderBrush="Black" BorderThickness="1,0,1,1" TextAlignment="Center">
                        <Paragraph FontWeight="Bold">Price</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>$120.00</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>$1000.00</Paragraph>
                    </TableCell>
                    <TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
                        <Paragraph>$130.00</Paragraph>
                    </TableCell>
                </TableRow>
            </TableRowGroup>
        </Table>
    </FlowDocument>
</FlowDocumentReader>

This page is full of usefull examples about this: FlowDocument with Table

查看更多
登录 后发表回答