Creating a simple Table in WPF?

别来无恙 提交于 2020-01-01 02:47:49

问题


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:

Any ideas?


回答1:


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.

<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




回答2:


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



来源:https://stackoverflow.com/questions/7385163/creating-a-simple-table-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!