C#FlowDocument单元格宽度问题

时间:2014-10-06 21:00:09

标签: c# wpf flowdocument

我无法为添加到流文档的每一行调整单元格大小。我想手动设置流文档单元格的宽度。此代码是从Microsoft example

获取并修改的

代码:

internal static FlowDocument GetPicklistFlowDoc()
    {
        DataTable daTable = new DataTable();
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["PicklistConnection"].ConnectionString))
        using (var cmd = new SqlCommand("GetCurrentPicklistItems", con))
        using (var da = new SqlDataAdapter(cmd))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            da.Fill(daTable);
        }

        FlowDocument flowDoc = new FlowDocument();
        flowDoc.IsColumnWidthFlexible = true;
        // Create the Table...
        Table table1 = new Table();

        // ...and add it to the FlowDocument Blocks collection.
        flowDoc.Blocks.Add(table1);


        // Set some global formatting properties for the table.
        //table1.CellSpacing = 15;
        //table1.Background = Brushes.White;

        int numberOfColumns = 6;

        // Create and add an empty TableRowGroup to hold the table's Rows.
        table1.RowGroups.Add(new TableRowGroup());

        // Add the first (title) row.
        table1.RowGroups[0].Rows.Add(new TableRow());

        // Alias the current working row for easy reference.
        TableRow currentRow = table1.RowGroups[0].Rows[0];

        // formatting for the title row.
        currentRow.Background = Brushes.Silver;
        currentRow.FontSize = 24;
        currentRow.FontWeight = System.Windows.FontWeights.Bold;

        // Add the header row with content, 
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Picklist" + DateTime.Now.ToString()))));
        // and set the row to span all 6 columns.
        currentRow.Cells[0].ColumnSpan = 6;

        // Add (header) row.
        table1.RowGroups[0].Rows.Add(new TableRow());
        currentRow = table1.RowGroups[0].Rows[1];

        // formatting for the header row.
        currentRow.FontSize = 18;
        currentRow.FontWeight = FontWeights.Bold;


        // Add cells with content to the second row.
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Code"))));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Qty"))));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Location"))));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Last 1"))));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Issue"))));
        currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Store"))));
        currentRow.Cells[0].ColumnSpan = numberOfColumns;
        //currentRow.Cells[0].BorderThickness = new Thickness(0, 0, 0, 1);
        //currentRow.Cells[0].BorderBrush = Brushes.Black;

        // Add the another row. and formatting
        table1.RowGroups[0].Rows.Add(new TableRow());
        currentRow = table1.RowGroups[0].Rows[2];                
        currentRow.FontSize = 14;
        currentRow.FontWeight = FontWeights.Normal;
        currentRow.FontFamily = new FontFamily("Arial");
        int z = 3;

        foreach (DataRow dataRow in daTable.Rows)
        {
            double quantity = (double)dataRow[1];
            string location = dataRow[3].ToString();


                if (quantity > 1)
                {

                    currentRow.Background = Brushes.Silver;

                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[0].ToString()))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[1].ToString()))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(location))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[2].ToString()))));

                    currentRow.Cells[0].ColumnSpan = numberOfColumns;

                    for (int i = 0; i <= numberOfColumns - 1; i++)
                    {

                        currentRow.Cells[i].BorderThickness = new Thickness(1, 1, 1, 1);
                        currentRow.Cells[i].BorderBrush = Brushes.Black;
                    }



                }
                else
                {
                    //currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[0].ToString()))));

                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[0].ToString()))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[1].ToString()))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[3].ToString()))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(""))));
                    currentRow.Cells.Add(new TableCell(new Paragraph(new Run(dataRow[2].ToString()))));
                    currentRow.Cells[0].ColumnSpan = numberOfColumns;
                    for (int i = 0; i <= numberOfColumns - 1; i++)
                    {

                        currentRow.Cells[i].BorderThickness = new Thickness(1, 1, 1, 1);
                        currentRow.Cells[i].BorderBrush = Brushes.Black;
                    }

                }

            table1.RowGroups[0].Rows.Add(new TableRow());
            currentRow = table1.RowGroups[0].Rows[z];

            location = "";

            z++;
        }


        z = 3;


        return flowDoc;
    }

我想手动设置单元格宽度属性或将它们设置为autoWidth。

1 个答案:

答案 0 :(得分:1)

在相关问题栏中发帖后找到answer

 GridLengthConverter glc = new GridLengthConverter();
 table.Columns[0].Width = (GridLength)glc.ConvertFromString("300");
 table.Columns[1].Width = (GridLength)glc.ConvertFromString("50");
 table.Columns[2].Width = (GridLength)glc.ConvertFromString("50");