2010-03-01 3 views
14

J'ai une ListView dans mon application WPF qui est liée à une collection de tâches à effectuer (une liste de choses à faire). Je souhaite que l'utilisateur puisse imprimer sa liste et avoir créé le code suivant en fonction des consignes MSDN. (Ma première incursion dans l'impression)Pourquoi cette table flowdocument imprime-t-elle toujours 2 colonnes?

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; 
} 

Lorsque je tente d'imprimer avec le code suivant, je l'ai toujours ma page coupée en deux avec 2 colonnes (contenant chacune les 3 colonnes du tableau). J'ai essayé différentes valeurs GridLength mais je n'ai pas eu de succès.

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

Répondre

19

Je suppose que le meilleur moyen d'obtenir une réponse est d'abandonner et de demander, alors vous le trouvez vous-même.

Le problème était dans la ligne pour imprimer les pages, pas le flowdoc lui-même. Par défaut, ils impriment avec 2 colonnes. Le code corrigé est (ce traite également de la marge et zone imprimable):

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"); 

} 

D'ailleurs je trouve cela dans « Pro WPF en C# 2008 » de Matthew MacDonald que je recommande fortement.

3

Merci pour l'info. Je l'ai fixé en réglant simplement la largeur de la colonne comme: flowDoc.ColumnWidth = pageSize.Width

FYI ne jamais essayer d'obtenir de l'aide de netframeworkdev ou .Net Framework Développer b/c ils n'ont jamais de bonnes réponses. J'aurais aimé que mon moteur de recherche me pointe sur StackOverflow plutôt que sur ce site sans valeur. StackOverflow a toujours les réponses. :) Merci encore.

(Souhait vous pouvez simplement bloquer les sites de montrer jamais dans vos résultats de recherche, vous savez comment faire s'il vous plaît me dire.)

+0

Je ne peux pas vous dire combien de fois je voulais me cacher un site de mon Résultats de la recherche. –