WPF 文档:使表格单元格边框正确

发布于 2024-08-10 05:37:54 字数 2499 浏览 1 评论 0原文

我在 MS Word 中创建了这张图片,并尝试使用文档在我的 WPF 应用程序中复制该样式。首先是“来自”:

替代文本 http://img337.imageshack.us/img337/1275/correntborder.png< /a>

接下来我尝试复制:

alt text http://img156.imageshack.us/img156/1711/extrawhiteborder .png

我的问题可能相当明显。我做错了什么?我在行分组或行上找不到填充属性。下面是我的代码:

    public override FlowDocument CreateDocumentSection(IInteractivityElement pElement)
    {
        var result = new FlowDocument();

        // show the header
        result.Blocks.Add(CreateHeading(pElement.Header));

        // we don't show anything else if there aren't any columns
        var nrColumns = pElement.GetIntegralData("CurrentColumnCount") ?? 0;
        if (nrColumns == 0) return result;

        Table mainTable = new Table();
        result.Blocks.Add(mainTable);

        // columns
        for (long tableIdx = 0; tableIdx < nrColumns; tableIdx++)
        {
            var newColumn = new TableColumn();
            mainTable.Columns.Add(newColumn);
        }

        // row group for header
        TableRowGroup rowGroup = new TableRowGroup();
        mainTable.RowGroups.Add(rowGroup);

        // row for header
        TableRow headerRow = new TableRow();
        headerRow.Background = new SolidColorBrush(Color.FromRgb(79, 129, 189));
        headerRow.Foreground = new SolidColorBrush(Colors.White);
        rowGroup.Rows.Add(headerRow);

        // add columns for each header cell
        for (long tableIdx = 0; tableIdx < nrColumns; tableIdx++)
        {
            var headerNameKey = CreateColumnNameKey(tableIdx);
            TableCell headerCell = new TableCell(new Paragraph(new Run(pElement.GetStringData(headerNameKey))));
            headerRow.Cells.Add(headerCell);
        }

        TableRow emptyRow = new TableRow();
        emptyRow.Foreground = new SolidColorBrush(Colors.Gray);
        rowGroup.Rows.Add(emptyRow);

        TableCell emptyInstructionCell = new TableCell();
        emptyInstructionCell.BorderBrush = new SolidColorBrush(Color.FromRgb(79, 129, 189));
        emptyInstructionCell.BorderThickness = new Thickness(1.0);
        emptyInstructionCell.ColumnSpan = Convert.ToInt32(nrColumns);
        emptyInstructionCell.Blocks.Add(new Paragraph(new Run(pElement.Instruction)));
        emptyRow.Cells.Add(emptyInstructionCell);

        return result;
    }

I created this pic in MS Word and I am trying to replicate the style in my WPF app using the Documents. First the 'from':

alt text http://img337.imageshack.us/img337/1275/correntborder.png

Next my attempt to replicate:

alt text http://img156.imageshack.us/img156/1711/extrawhiteborder.png

My question is probably rather obvious. What am I doing wrong? I can't find a padding property on the rowgrouping or the row. Below is my code:

    public override FlowDocument CreateDocumentSection(IInteractivityElement pElement)
    {
        var result = new FlowDocument();

        // show the header
        result.Blocks.Add(CreateHeading(pElement.Header));

        // we don't show anything else if there aren't any columns
        var nrColumns = pElement.GetIntegralData("CurrentColumnCount") ?? 0;
        if (nrColumns == 0) return result;

        Table mainTable = new Table();
        result.Blocks.Add(mainTable);

        // columns
        for (long tableIdx = 0; tableIdx < nrColumns; tableIdx++)
        {
            var newColumn = new TableColumn();
            mainTable.Columns.Add(newColumn);
        }

        // row group for header
        TableRowGroup rowGroup = new TableRowGroup();
        mainTable.RowGroups.Add(rowGroup);

        // row for header
        TableRow headerRow = new TableRow();
        headerRow.Background = new SolidColorBrush(Color.FromRgb(79, 129, 189));
        headerRow.Foreground = new SolidColorBrush(Colors.White);
        rowGroup.Rows.Add(headerRow);

        // add columns for each header cell
        for (long tableIdx = 0; tableIdx < nrColumns; tableIdx++)
        {
            var headerNameKey = CreateColumnNameKey(tableIdx);
            TableCell headerCell = new TableCell(new Paragraph(new Run(pElement.GetStringData(headerNameKey))));
            headerRow.Cells.Add(headerCell);
        }

        TableRow emptyRow = new TableRow();
        emptyRow.Foreground = new SolidColorBrush(Colors.Gray);
        rowGroup.Rows.Add(emptyRow);

        TableCell emptyInstructionCell = new TableCell();
        emptyInstructionCell.BorderBrush = new SolidColorBrush(Color.FromRgb(79, 129, 189));
        emptyInstructionCell.BorderThickness = new Thickness(1.0);
        emptyInstructionCell.ColumnSpan = Convert.ToInt32(nrColumns);
        emptyInstructionCell.Blocks.Add(new Paragraph(new Run(pElement.Instruction)));
        emptyRow.Cells.Add(emptyInstructionCell);

        return result;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独木成林 2024-08-17 05:37:54

遗憾的是,您无法在 FlowDocument 中设置 TableRow 的边框。它仅适用于TableTableCell。我什至想知道为什么没有提供这一点。

虽然实现行边框效果的一种方法是将所有单元格的边框与 BorderThickness 结合使用,并将容器 TableCellSpacing 设置为0. 例如:

table.CellSpacing = 0;
...
cellLeft.BorderThickness= new Thickness(1, 1, 0, 1);
...
cellCenter.BorderThickness= new Thickness(0, 1);
...
cellRight.BorderThickness= new Thickness(0, 1, 1, 1);

Unfortunately you cannot set the border for a TableRow in a FlowDocument. It is only available for the Table or the TableCell. Even I wonder why this was not provided.

Although one way to achieve a row border effect is to use border of all cells in conjunction with BorderThickness, and setting CellSpacing of the container Table to 0. For eg:

table.CellSpacing = 0;
...
cellLeft.BorderThickness= new Thickness(1, 1, 0, 1);
...
cellCenter.BorderThickness= new Thickness(0, 1);
...
cellRight.BorderThickness= new Thickness(0, 1, 1, 1);
红玫瑰 2024-08-17 05:37:54

Yogesh,很抱歉这么晚才回答,但我刚刚回答这个问题。也许答案可以帮助其他人。

在这种特殊情况下,您需要将 table.BorderThickness 设置为 1,将 table.CellSpacing 设置为 0,以及每个单元格的顶部或底部边框。

为了避免将每个单元格的厚度设置为(0,1,0,0),您可以使用样式。有很多方法可以做到这一点,但我将向您展示一种简单的方法。在您的 App.xaml 中,编写以下内容:

<Application x:Class="YourNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:doc="clr-namespace:System.Windows.Documents;assembly=PresentationFramework">

    <Application.Resources>
        <Style TargetType="doc:TableCell" >
            <Setter Property="BorderBrush" Value="Blue" />
            <Setter Property="BorderThickness" Value="0,1,0,0" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Padding" Value="2" />
        </Style>        
    </Application.Resources>
</Application>

之后,将应用程序字典合并到您的文档或表格中,内容如下:

mainTable.Resources.MergedDictionaries.Add(App.Current.Resources);

您可以为整个文档、单个表格甚至单个行或单元格设置样式。

Yogesh, sorry for this late answer but I just arrived to this question. Maybe the answer can help others.

In this particular case, you need to set a table.BorderThickness to 1, table.CellSpacing to 0, and the either top OR bottom border for each cell.

In order to avoid to set the thickness to (0,1,0,0) for each cell, you can use styles. There are many ways to do this, but I will show you a simple one. In your App.xaml, write the following:

<Application x:Class="YourNamespace.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:doc="clr-namespace:System.Windows.Documents;assembly=PresentationFramework">

    <Application.Resources>
        <Style TargetType="doc:TableCell" >
            <Setter Property="BorderBrush" Value="Blue" />
            <Setter Property="BorderThickness" Value="0,1,0,0" />
            <Setter Property="FontSize" Value="12" />
            <Setter Property="Padding" Value="2" />
        </Style>        
    </Application.Resources>
</Application>

After that, merge the application dictionary into your document or table, with something like:

mainTable.Resources.MergedDictionaries.Add(App.Current.Resources);

You can have styles for the whole document, an individual table and even an individual row or cell.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文