■ Table 엘리먼트를 사용해 FlowDocument 엘리먼트에서 테이블을 만드는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <FlowDocumentReader> <FlowDocument Name="flowDocument" /> </FlowDocumentReader> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
using System.Windows; using System.Windows.Documents; using System.Windows.Media; namespace TestProject; /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); TableColumn tableColumn1 = new TableColumn { Width = GridLength.Auto }; TableColumn tableColumn2 = new TableColumn { Width = GridLength.Auto }; TableColumn tableColumn3 = new TableColumn { Width = GridLength.Auto }; Paragraph headerCellParagraph1 = new Paragraph(new Run("Header 1")); Paragraph headerCellParagraph2 = new Paragraph(new Run("Header 2")); Paragraph headerCellParagraph3 = new Paragraph(new Run("Header 3")); TableCell headerTableCell1 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3), Background = Brushes.LightGray, TextAlignment = TextAlignment.Center }; TableCell headerTableCell2 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3), Background = Brushes.LightGray, TextAlignment = TextAlignment.Center }; TableCell headerTableCell3 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3), Background = Brushes.LightGray, TextAlignment = TextAlignment.Center }; headerTableCell1.Blocks.Add(headerCellParagraph1); headerTableCell2.Blocks.Add(headerCellParagraph2); headerTableCell3.Blocks.Add(headerCellParagraph3); TableRow headerTableRow = new TableRow(); headerTableRow.Cells.Add(headerTableCell1); headerTableRow.Cells.Add(headerTableCell2); headerTableRow.Cells.Add(headerTableCell3); Paragraph dataCellParagraph1 = new Paragraph(new Run("Row 1, Cell 1")); Paragraph dataCellParagraph2 = new Paragraph(new Run("Row 1, Cell 2")); Paragraph dataCellParagraph3 = new Paragraph(new Run("Row 1, Cell 3")); TableCell dataTableCell1 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3) }; TableCell dataTableCell2 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3) }; TableCell dataTableCell3 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3) }; dataTableCell1.Blocks.Add(dataCellParagraph1); dataTableCell2.Blocks.Add(dataCellParagraph2); dataTableCell3.Blocks.Add(dataCellParagraph3); TableRow dataTableRow1 = new TableRow(); dataTableRow1.Cells.Add(dataTableCell1); dataTableRow1.Cells.Add(dataTableCell2); dataTableRow1.Cells.Add(dataTableCell3); Paragraph dataCellParagraph4 = new Paragraph(new Run("Row 2, Cell 1")); Paragraph dataCellParagraph5 = new Paragraph(new Run("Row 2, Cell 2")); Paragraph dataCellParagraph6 = new Paragraph(new Run("Row 2, Cell 3")); TableCell dataTableCell4 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3) }; TableCell dataTableCell5 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3) }; TableCell dataTableCell6 = new TableCell { BorderThickness = new Thickness(1), BorderBrush = Brushes.Black, Padding = new Thickness(3) }; dataTableCell4.Blocks.Add(dataCellParagraph4); dataTableCell5.Blocks.Add(dataCellParagraph5); dataTableCell6.Blocks.Add(dataCellParagraph6); TableRow dataTableRow2 = new TableRow(); dataTableRow2.Cells.Add(dataTableCell4); dataTableRow2.Cells.Add(dataTableCell5); dataTableRow2.Cells.Add(dataTableCell6); TableRowGroup tableRowGroup = new TableRowGroup(); tableRowGroup.Rows.Add(headerTableRow); tableRowGroup.Rows.Add(dataTableRow1 ); tableRowGroup.Rows.Add(dataTableRow2 ); Table table = new Table(); table.BorderThickness = new Thickness(1); table.BorderBrush = Brushes.Black; table.CellSpacing = 3; table.Columns.Add(tableColumn1); table.Columns.Add(tableColumn2); table.Columns.Add(tableColumn3); table.RowGroups.Add(tableRowGroup); this.flowDocument.Blocks.Add(table); } #endregion } |