2010-03-01 53 views
14

我在我的WPF應用程序中綁定了一組要執行的任務(待辦事項列表)的ListView。我希望用戶能夠打印他們的列表並根據MSDN指南創建了以下代碼。 (這是我第一次涉足印刷)爲什麼這個flowdocument表總是打印2列

public FlowDocument GetPrintDocument() 
{ 
    FlowDocument flowDoc = new FlowDocument(); 
    Table table = new Table(); 

    int numColumns = 3; 

    flowDoc.Blocks.Add(table); 

    for(int x=0;x<numColumns;x++) 
    { 
     table.Columns.Add(new TableColumn()); 
    } 
    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"); 

    table.RowGroups.Add(new TableRowGroup()); 

    table.RowGroups[0].Rows.Add(new TableRow()); 
    // store current working row for reference 
    TableRow currentRow = table.RowGroups[0].Rows[0]; 

    currentRow.FontSize = 16; 
    currentRow.FontWeight = FontWeights.Bold; 

    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Subject")))); 
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Due Date")))); 
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Urgency")))); 

    for (int i = 1; i < issues.Count+1; i++) 
    { 
     table.RowGroups[0].Rows.Add(new TableRow()); 
     currentRow = table.RowGroups[0].Rows[i]; 
     currentRow.FontSize = 12; 
     currentRow.FontWeight = FontWeights.Normal; 

     currentRow.Cells.Add(new TableCell 
          (new Paragraph 
          (new Run 
          (issues[i - 1].IssSubject)))); 
     currentRow.Cells.Add(new TableCell 
          (new Paragraph 
          (new Run 
          (issues[i - 1].IssDueDate.Date.ToString())))); 
     currentRow.Cells.Add(new TableCell 
          (new Paragraph 
          (new Run 
          (issues[i - 1].IssUrgency.ToString())))); 
    } 
    return flowDoc; 
} 

當我嘗試用下面的代碼,我總是有我的網頁分裂成兩派2列(每片含表的3列)進行打印。我嘗試了不同的GridLength值,但沒有成功。

printDialog.PrintDocument(((IDocumentPaginatorSource)StatusBoardViewModel 
       .GetPrintDocument()) 
       .DocumentPaginator 
      ,"Flow Document Print Job"); 

回答

19

我想最好的方式得到答案是放棄和問,然後你自己找到它。

問題在於打印頁面,而不是flowdoc本身。默認情況下,它們打印2列。更正後的代碼是(這也與邊距和可打印區域交易):

PrintDialog printDialog = new PrintDialog(); 

if (printDialog.ShowDialog() == true) 
{ 

    FlowDocument flowDoc = statusBoardViewModel.GetPrintDocument(); 

    flowDoc.PageHeight = printDialog.PrintableAreaHeight; 
    flowDoc.PageWidth = printDialog.PrintableAreaWidth; 
    flowDoc.PagePadding = new Thickness(25); 

    flowDoc.ColumnGap = 0; 

    flowDoc.ColumnWidth = (flowDoc.PageWidth - 
          flowDoc.ColumnGap - 
          flowDoc.PagePadding.Left - 
          flowDoc.PagePadding.Right); 

    printDialog.PrintDocument(((IDocumentPaginatorSource)flowDoc) 
          .DocumentPaginator, 
          "Task Manager Print Job"); 

} 

通過I「在C#2008年臨WPF」我強烈建議看到這個馬修·麥克唐納的方式。

3

感謝您的信息。我通過設置列寬來修正它: flowDoc.ColumnWidth = pageSize.Width

僅供參考,如果您從未嘗試從netframeworkdev或.Net Framework開發b/c獲得幫助,他們從來沒有很好的答案。我希望我的搜索引擎會指向我在StackOverflow而不是那個毫無價值的網站。 StackOverflow總是有答案。 :) 再次感謝。

(祝你可以只在搜索結果中顯示過封鎖的網站,你就知道該怎麼做,請告訴我。)

+0

我不能告訴你我是如何經常希望我可以隱藏網站我搜索結果。 – 2010-12-24 18:55:28