2017-10-24 3 views
0

マップとデータをデータグリッドに表示するユーザーコントロールが1つあります。C#を使用してwpfユーザーコントロールのプリントを取る方法は?

私は次のコードを試しましたが、動作しません。

 PrintDialog printDialog = new PrintDialog(); 

     // if user clicks on cancel button of print dialog box then no need to print the map control 
     if (!(bool)printDialog.ShowDialog()) 
     { 
      return; 
     } 
     else // this means that user click on the print button 
     { 
      // do nothing 
     } 

     this.Measure(new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight)); 
     this.Arrange(new Rect(new Point(20, 20), new Size(this.ActualWidth, this.ActualHeight))); 

     // print the map control 
     printDialog.PrintVisual(this, "Karte drucken"); 

問題:データグリッドは、多数のレコードを持っている場合、ユーザーコントロールがスクロールバーを取得しますが、ユーザーコントロールを印刷した後、ユーザーコントロールの唯一の目に見える部分が印刷されていない、我々は後に見ることができるデータの存在スクロールダウンします。私はユーザーコントロールの全内容を印刷したいと思います。

これには解決策はありますか?また、どのようにwpfで印刷プレビューを見ることができますか?

答えて

1

次のリンクを確認してください。役立つはずです。ヘルプswapnilためPrint Preview WPF

コード

PrintDialog printDlg = new System.Windows.Controls.PrintDialog(); 
if (printDlg.ShowDialog() == true) 
    { 
    //get selected printer capabilities 
    System.Printing.PrintCapabilities capabilities = printDlg.PrintQueue.GetPrintCapabilities(printDlg.PrintTicket); 

    //get scale of the print wrt to screen of WPF visual 
    double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth/this.ActualWidth, capabilities.PageImageableArea.ExtentHeight/
      this.ActualHeight); 

    //Transform the Visual to scale 
    this.LayoutTransform = new ScaleTransform(scale, scale); 

    //get the size of the printer page 
    Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight); 

    //update the layout of the visual to the printer page size. 
    this.Measure(sz); 
    this.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz)); 

    //now print the visual to printer to fit on the one page. 
    printDlg.PrintVisual(this, "First Fit to Page WPF Print"); 

} 
+0

おかげで、私は「userControl.Arrange(新しいのRect(新しいポイント(50、50)、新しいサイズ(userControl.ActualWidth、userControl.ActualHeightを使用して問題を解決しました)))); ") "新しいRect(新しい点(50,50)、新しいサイズ(userControl.ActualWidth、userControl.ActualHeight))))の代わりに" dategrid.Arrange "私は今新しい問題に直面している、私は質問を更新しました – pankaj

+0

@pankajこのスレッドを見てください[リンク](https://stackoverflow.com/questions/3009575/wpf-best-method-for-printing-paginated-datagrids) –

+0

@pankajもこのmsdnの記事を参照してください。[リンク](https://social.msdn.microsoft.com/Forums/vstudio/en-US/be2edad6-f075-4295-ab22-3657eba78043/how-to-print-the- dlg.PrintVisual(panel.Content、 "Print Button"); 'dlg.PrintVisual(panel.Content、" Print Button ");これらの行を探します。 –

関連する問題