打印 WPF DataGrid 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13025430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing WPF DataGrid Contents
提问by tribal
How can I print the contents of a DataGrid. I have checked out the following post How can I produce a "print preview" of a FlowDocument in a WPF application?and it generates only the visible portion of the grid not the scrollable parts. I would need the preview to have multiple pages and I thiink I should be using the FlowDocument but I am not sure how to go about it. Any ideas would be appreciated.
如何打印 DataGrid 的内容。我已经查看了以下帖子 如何在 WPF 应用程序中生成 FlowDocument 的“打印预览”?它只生成网格的可见部分,而不是可滚动部分。我需要有多个页面的预览,我认为我应该使用 FlowDocument,但我不知道如何去做。任何想法,将不胜感激。
回答by Sisyphe
I had this issue some time ago. I wrote a method that generates a System.Windows.Documents.Tablefrom the DataGrid. I put it in a FlowDocument and generated a fixed document thanks to an XpsDocumentWriter. You will then have a full paginated view of your DataGrid that you can visualize in a DocumentViewer
我前段时间遇到了这个问题。我编写了一个从 DataGrid生成System.Windows.Documents.Table的方法。由于XpsDocumentWriter ,我将它放在 FlowDocument 中并生成了一个固定文档。然后,您将拥有 DataGrid 的完整分页视图,您可以在DocumentViewer 中对其进行可视化
回答by Denny Weinberg
Use this: it works fine.
使用这个:它工作正常。
public class UIPrinter
{
#region Properties
public Int32 VerticalOffset { get; set; }
public Int32 HorizontalOffset { get; set; }
public String Title { get; set; }
public UIElement Content { get; set; }
#endregion
#region Initialization
public TimelinePrinter()
{
HorizontalOffset = 20;
VerticalOffset = 20;
Title = "Print " + DateTime.Now.ToMyStringWithTime();
}
#endregion
#region Methods
public Int32 Print()
{
var dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
//---FIRST PAGE---//
// Size the Grid.
Content.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
Size sizeGrid = Content.DesiredSize;
//check the width
if (sizeGrid.Width > dlg.PrintableAreaWidth)
{
MessageBoxResult result = MessageBox.Show(Properties.Resources.s_EN_Question_PrintWidth, "Print", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.No)
throw new PrintAborted(Properties.Resources.s_EN_Info_PrintingAborted);
}
// Position of the grid
var ptGrid = new Point(HorizontalOffset, VerticalOffset);
// Layout of the grid
Content.Arrange(new Rect(ptGrid, sizeGrid));
//print
dlg.PrintVisual(Content, Title);
//---MULTIPLE PAGES---//
double diff;
int i = 1;
while ((diff = sizeGrid.Height - (dlg.PrintableAreaHeight - VerticalOffset*i)*i) > 0)
{
//Position of the grid
var ptSecondGrid = new Point(HorizontalOffset, -sizeGrid.Height + diff + VerticalOffset);
// Layout of the grid
Content.Arrange(new Rect(ptSecondGrid, sizeGrid));
//print
int k = i + 1;
dlg.PrintVisual(Content, Title + " (Page " + k + ")");
i++;
}
return i;
}
throw new PrintAborted(Properties.Resources.s_EN_Info_PrintingAborted);
}
#endregion
}
It prints the Datagrid or any other Control with the selected printer on multiple pages...
它使用选定的打印机在多页上打印 Datagrid 或任何其他控件...
Usage:
用法:
MessageBoxResult result = MessageBox.Show(Properties.Resources.s_EN_Question_Print, "Print", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
try
{
var border = VisualTreeHelper.GetChild(MyDataGrid, 0) as Decorator;
if (border != null)
{
var scrollViewer = border.Child as ScrollViewer;
if (scrollViewer != null)
{
scrollViewer.ScrollToTop();
scrollViewer.ScrollToLeftEnd();
}
}
Title = _initialTitle + " - " + Properties.Resources.s_EN_Info_Printing;
var myPrinter = new UIPrinter{ Title = Title, Content = PrintGrid };
int nbrOfPages = myPrinter.Print();
Title = _initialTitle + " - " + Properties.Resources.s_EN_Info_PrintingDone + " (" + nbrOfPages + " Pages)";
}
catch (PrintAborted ex)
{
Title = _initialTitle + " - " + ex.Message;
}
}
EDIT: I placed my Datagrid on a simple grid containing a header control to have a header on my paper.
编辑:我将我的 Datagrid 放在一个包含标题控件的简单网格上,以便在我的论文上有一个标题。

