猿问

C# WPF 以编程方式添加表格边框

我正在创建一个需要备份的程序。在仪表板上,我有一个以编程方式创建的表。不过我想要:

  1. 表格边框为#cccccc

  2. Headcell 具有顶部边框 #ffffff

  3. 每行都有底部边框 #cccccc

我的程序截图:

绘制我想要的表格外观:

http://img4.mukewang.com/64bb96c30001708c06050174.jpg

我的代码(https://github.com/europa9/Windows_Backup_Folders_to_External_Disks_Csharp):



吃鸡游戏
浏览 320回答 1
1回答

MM们

您可以直接在元素上设置属性Block:var brushConverter = new BrushConverter();var tableBorderBrush = brushConverter.ConvertFrom("#CCCCCC");tableBorderBrush.Freeze();var headerCellBorderBrush = brushConverter.ConvertFrom("#FFFFFF");headerCellBorderBrush.Freeze();private void drawLogTable() {  Table oTable = new Table() { BorderThickness = new Thickness(1), BorderBrush = tableBorderBrush }  // Add the header row with content,  currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Date time"))) { BorderThickness = new Thickness(0, 1, 0, 0), BorderBrush = headerCellBorderBrush });  currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Directory"))) { BorderThickness = new Thickness(0, 1, 0, 0), BorderBrush = headerCellBorderBrush });  currentRow.Cells.Add(new TableCell(new Paragraph(new Run("File"))) { BorderThickness = new Thickness(0, 1, 0, 0), BorderBrush = headerCellBorderBrush });  // Read file and add rows  ...  foreach (string line in existingFoldersArray)  {    ...    // Add new row    oTable.RowGroups[0].Rows.Add(new TableRow());    currentRow = oTable.RowGroups[0].Rows[countLines];    ...    //Add the row's cells with their borders set    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dateTime))) { BorderThickness = new Thickness(0, 0, 0, 1), BorderBrush = tableBorderBrush });    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(directory))) { BorderThickness = new Thickness(0, 0, 0, 1), BorderBrush = tableBorderBrush });    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(file))) { BorderThickness = new Thickness(0, 0, 0, 1), BorderBrush = tableBorderBrush });    ...  }  ...}行的边框是单元格的边框。要设置行的边框,TableCell必须设置每行的边框。我强烈建议使用 XAML 来DataTemplates完成此任务。这更容易、更易读、更容易理解并且更灵活。您还可以使用 XAML 设计器的所见即所得功能。
随时随地看视频慕课网APP
我要回答