在 WPF 中创建一个简单的表?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7385163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a simple Table in WPF?
提问by Mohammad Sepahvand
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:
我想知道是否有一种方法(任何组件/控件)可以让我在我的应用程序窗口中绘制一个简单的 Microsoft Word 样式表。像这样的东西:
Any ideas?
有任何想法吗?
回答by Fredrik Hedblad
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
这取决于您想如何使用它。的任一个的使用ItemsControl
(如DataGrid
,ListView
等),具有这样做直接Grid
面板(所建议的其他的答案),或者使用一FlowDocument
FlowDocument
allows you to specify Tables, Rows and Columns. You can also select several cells at once for Copy/Paste etc.
FlowDocument
允许您指定表、行和列。您还可以一次选择多个单元格进行复制/粘贴等。
<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>0.00</Paragraph>
</TableCell>
<TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
<Paragraph>00.00</Paragraph>
</TableCell>
<TableCell BorderBrush="Black" BorderThickness="0,0,1,1">
<Paragraph>0.00</Paragraph>
</TableCell>
</TableRow>
</TableRowGroup>
</Table>
</FlowDocument>
</FlowDocumentReader>
This page is full of usefull examples about this: FlowDocument with Table
此页面充满了关于此的有用示例:FlowDocument with Table
回答by Maxim V. Pavlov
I would recommend starting with WPF ToolkitDataGrid control.
我建议从WPF ToolkitDataGrid 控件开始。
Here is an ok tutorial on how to use it: http://www.switchonthecode.com/tutorials/using-the-wpf-toolkit-datagrid
这是一个关于如何使用它的不错的教程:http: //www.switchonthecode.com/tutorials/using-the-wpf-toolkit-datagrid