1:先把页面DataGrid上的数据导入到一个另外一个空DataGrid中:
for (int i = 0; i < StatisticsDetail.Columns.Count; i++) { DataGridTextColumn tmpColumn = new DataGridTextColumn(); DataGridTextColumn column = (DataGridTextColumn)StatisticsDetail.Columns[i]; tmpColumn.Header = column.Header.ToString(); System.Windows.Data.Binding bind; if (column.Binding != null) { bind = new System.Windows.Data.Binding(); bind.Path = column.Binding.Path; bind.Converter = column.Binding.Converter; } else bind = new System.Windows.Data.Binding(); tmpColumn.Binding = bind; grid.Columns.Add(tmpColumn); } grid.ItemsSource = StatisticsDetail.ItemsSource; //开始打印 Print(grid,documentName);
2:在采用Silverlight中的Print方法打印
private static void Print(UIElement source,string documentName) { var doc = new PrintDocument(); var offsetY = 0d; var totalHeight = 0d; var canvas = new Canvas(); canvas.Children.Add(source); try { doc.PrintPage += (s,e) => { e.PageVisual = canvas; canvas.Margin = new Thickness(50); if (totalHeight == 0) { totalHeight = source.DesiredSize.Height; } Canvas.SetTop(source,-offsetY); offsetY += e.PrintableArea.Height; //是否多页打印 e.HasMorePages = offsetY <= totalHeight; }; doc.Print(documentName); } catch { } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。