在WPF中创建一个简单的表格?

15

我想知道是否有一种方法(任何组件/控件)可以让我在应用程序窗口中绘制一个类似于Microsoft Word简单表格的样式。就像这样:

示例表格

有什么想法吗?

2个回答

19

这取决于您想如何使用它。您可以使用其中一个ItemsControl(如DataGridListView等),直接使用Grid面板(建议参考其他答案),或使用FlowDocument

FlowDocument允许您指定表格、行和列。您还可以同时选择多个单元格进行复制/粘贴等操作。

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>

这个页面充满了关于此的有用示例:带表格的FlowDocument


+1 我之前不知道 FlowDocument。大约18个月前,我完成了一个中等规模的WPF应用程序,自那以后就没有再做过任何事情。我的Xaml技能已经生疏了 :( - Binary Worrier
由于某种奇怪的原因,当我创建一个表格时,会出现搜索字段和不同的视图。我该如何删除所有这些信息,只包括表格而没有额外的花哨功能? - Kala J

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接