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

Pie*_*eed 9 wpf formatting document

我在MS Word中创建了这张图片,我正在尝试使用Documents在我的WPF应用程序中复制样式.首先是"来自":

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

接下来我尝试复制:

替代文字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;
    }
Run Code Online (Sandbox Code Playgroud)

Yog*_*esh 9

很遗憾,您无法为a TableRow中的a 设置边框FlowDocument.它仅适用于TableTableCell.甚至我想知道为什么没有提供这个.

虽然实现行边框效果的一种方法是使用所有单元格的边框BorderThickness,并将CellSpacing容器设置Table为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);
Run Code Online (Sandbox Code Playgroud)


小智 6

Yogesh,对这个迟到的回答感到抱歉,但我刚刚回答这个问题.也许答案可以帮助别人.

在这种特殊情况下,您需要将table.BorderThickness设置为1,将table.CellSpacing设置为0,并将每个单元格的顶部或底部边框设置为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>
Run Code Online (Sandbox Code Playgroud)

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

mainTable.Resources.MergedDictionaries.Add(App.Current.Resources);
Run Code Online (Sandbox Code Playgroud)

您可以为整个文档,单个表甚至单个行或单元格创建样式.