Creating a simple Table in WPF?

后端 未结 2 1106
无人及你
无人及你 2021-02-05 14:03

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:

相关标签:
2条回答
  • 2021-02-05 14:14

    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

    0 讨论(0)
  • 2021-02-05 14:15

    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

    0 讨论(0)
提交回复
热议问题