为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
吃鸡游戏 2023-07-22 16:43:11
我正在创建一个需要备份的程序。在仪表板上,我有一个以编程方式创建的表。不过我想要:表格边框为#ccccccHeadcell 具有顶部边框 #ffffff每行都有底部边框 #cccccc我的程序截图:绘制我想要的表格外观:我的代码(https://github.com/europa9/Windows_Backup_Folders_to_External_Disks_Csharp):
查看完整描述

1 回答

?
MM们

TA贡献1886条经验 获得超2个赞

您可以直接在元素上设置属性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 设计器的所见即所得功能。


查看完整回答
反对 回复 2023-07-22
  • 1 回答
  • 0 关注
  • 212 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信