none
PagedCollectionView的MoveToNextPage怎么用? RRS feed

  • 问题

  • 这两天在体验PrintDocument , 试着打印DataGrid 。

    PagedCollectionView pcv = brow.ItemsSource as PagedCollectionView;
    pcv.MoveToFirstPage();

    pcv.MoveToNextPage();

    执行后界面并没有到第二页,请问MoveToNextPage怎么用?

    哪儿能找到多页打印的例子?

     

    2010年5月17日 8:41

答案

  • 终于把它搞出来了,贴出来与大家分享,办法较土,请楼上指正,见笑了:

            public void _printDataGrid( ChildWindow form, DataGrid brow , int pagesizeMax )
            {
                System.Windows.Printing.PrintDocument docToPrint = new System.Windows.Printing.PrintDocument();
                PagedCollectionView pcv = brow.ItemsSource as PagedCollectionView;
                if (pcv == null)
                {
                    MessageBox.Show("表格里无数据!");
                    return;
                }

                // 保存原来状态
                Thickness a0 = form.Margin;
                double a1 = form.Height;
                double a2 = form.Width;
                Thickness c0 = brow.Margin;
                double c1 = brow.Height;
                double c2 = brow.Width;
                int c3 = pcv.PageSize;

                int pagesize;
                if (pagesizeMax <= 0)
                    pagesize = 37;// 每页行数最大值缺省37
                else
                    pagesize = pagesizeMax;
                int count = pcv.Count;  // 总记录数
                int pagen = 0;               // 共几页(当==0时表示还未计算)
                int pagen2 = 1;   
                docToPrint.PrintPage += (s, e) =>
                {
                     
                    form.Height = e.PrintableArea.Height * 2;   

                    form.Width = e.PrintableArea.Width;     
                    brow.Margin = new Thickness(0, 0, 0, 0);

                    if (pagen == 0)
                    {
                        while (pagesize > 0)
                        {
                            pcv.PageSize = pagesize;
                            pcv.MoveToFirstPage();
                            brow.UpdateLayout();

                            if (brow.DesiredSize.Height < e.PrintableArea.Height)
                            {
                                pagen = (int)((count + pagesize - 1) / pagesize);
                                break;
                            }
                            pagesize--;
                        }
                    }

                    if (pagen2 < pagen)
                        e.HasMorePages = true;
                    else
                        e.HasMorePages = false;
                    e.PageVisual = brow;

                    pcv.MoveToPage(pagen2 - 1);
                    brow.UpdateLayout();
                    pagen2++;
                };
                docToPrint.EndPrint += (s, e) =>
                {
                    pcv.PageSize = c3;
                    form.Margin = a0;
                    form.Height = a1;
                    form.Width = a2;
                    brow.Margin = c0;
                    brow.Height = c1;
                    brow.Width = c2;
                    brow.UpdateLayout();
                };
                docToPrint.Print("Sunpro Print");
            }

    2010年5月19日 0:57

全部回复

  •  

    找到了一点线索:应该先要设定每页行数 pcv.PageSize = 30;

    问题是如何知道目前的可见行数呢?

    2010年5月17日 8:59
  • 你具体的需求是什么?为什么想知道可见行数?这个跟PageSize是没有关系的。你是不想滚动条出现吗?因为这个DataGrid.Height和DataGrid.Row.Height都是你可以控制的,所以其实你可以自己通过设定来调整。

    2010年5月17日 18:14
  • 为了打印DataGrid, 所以要翻页。

    打印DataGrid有什么好的方法吗?

     

    2010年5月17日 23:58
  • 终于把它搞出来了,贴出来与大家分享,办法较土,请楼上指正,见笑了:

            public void _printDataGrid( ChildWindow form, DataGrid brow , int pagesizeMax )
            {
                System.Windows.Printing.PrintDocument docToPrint = new System.Windows.Printing.PrintDocument();
                PagedCollectionView pcv = brow.ItemsSource as PagedCollectionView;
                if (pcv == null)
                {
                    MessageBox.Show("表格里无数据!");
                    return;
                }

                // 保存原来状态
                Thickness a0 = form.Margin;
                double a1 = form.Height;
                double a2 = form.Width;
                Thickness c0 = brow.Margin;
                double c1 = brow.Height;
                double c2 = brow.Width;
                int c3 = pcv.PageSize;

                int pagesize;
                if (pagesizeMax <= 0)
                    pagesize = 37;// 每页行数最大值缺省37
                else
                    pagesize = pagesizeMax;
                int count = pcv.Count;  // 总记录数
                int pagen = 0;               // 共几页(当==0时表示还未计算)
                int pagen2 = 1;   
                docToPrint.PrintPage += (s, e) =>
                {
                     
                    form.Height = e.PrintableArea.Height * 2;   

                    form.Width = e.PrintableArea.Width;     
                    brow.Margin = new Thickness(0, 0, 0, 0);

                    if (pagen == 0)
                    {
                        while (pagesize > 0)
                        {
                            pcv.PageSize = pagesize;
                            pcv.MoveToFirstPage();
                            brow.UpdateLayout();

                            if (brow.DesiredSize.Height < e.PrintableArea.Height)
                            {
                                pagen = (int)((count + pagesize - 1) / pagesize);
                                break;
                            }
                            pagesize--;
                        }
                    }

                    if (pagen2 < pagen)
                        e.HasMorePages = true;
                    else
                        e.HasMorePages = false;
                    e.PageVisual = brow;

                    pcv.MoveToPage(pagen2 - 1);
                    brow.UpdateLayout();
                    pagen2++;
                };
                docToPrint.EndPrint += (s, e) =>
                {
                    pcv.PageSize = c3;
                    form.Margin = a0;
                    form.Height = a1;
                    form.Width = a2;
                    brow.Margin = c0;
                    brow.Height = c1;
                    brow.Width = c2;
                    brow.UpdateLayout();
                };
                docToPrint.Print("Sunpro Print");
            }

    2010年5月19日 0:57