打印在wpf中动态生成的网格

时间:2011-05-18 06:31:00

标签: wpf grid

我想打印一个动态生成的网格。

意味着,在“打印”按钮的单击事件中,我生成一个网格,然后我想打印该网格。

这是我的代码,

private void btnPrint_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog Objprint = new System.Windows.Controls.PrintDialog();
        if (Objprint.ShowDialog() == true)
        {

            System.Printing.PrintCapabilities capabilities = Objprint.PrintQueue.GetPrintCapabilities(Objprint.PrintTicket);

            double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / this.ActualWidth, capabilities.PageImageableArea.ExtentHeight / this.ActualHeight);

            #region "Make a grid For Printing"
            Grid objgrid = new Grid();
            objgrid.Name = "GridForPrinting";
            objgrid.Width = 1000;
            objgrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            objgrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;

            objgrid.RowDefinitions.Add(new RowDefinition());

            TextBlock objtext = new TextBlock();
            objtext.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            objtext.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            objtext.Text = "SUPERIOR COURT OF CALIF COUNTY OF SAN BERNARDINO";
            Grid.SetRow(objtext, 0);
            objgrid.Children.Add(objtext);

            #endregion

            Objprint.PrintVisual(objgrid, "Case Summary");
        }
    }

这段代码给我打印空白页。

我该怎么做?

2 个答案:

答案 0 :(得分:3)

我在这里得到答案, 从下面的代码我得到我想要做的...

void PrintOnClick(object sender, RoutedEventArgs args)
    {
        PrintDialog dlg = new PrintDialog();

        if ((bool)dlg.ShowDialog().GetValueOrDefault())
        {
            // Create Grid panel.
            Grid grid = new Grid();

            // Define 5 auto-sized rows and columns.
            for (int i = 0; i < 5; i++)
            {
                ColumnDefinition coldef = new ColumnDefinition();
                coldef.Width = GridLength.Auto;
                grid.ColumnDefinitions.Add(coldef);

                RowDefinition rowdef = new RowDefinition();
                rowdef.Height = GridLength.Auto;
                grid.RowDefinitions.Add(rowdef);
            }

            // Give the Grid a gradient brush.
            grid.Background = 
                new LinearGradientBrush(Colors.Black, Colors.White,
                                        new Point(0, 0), new Point(1, 1));

            // Every program needs some randomness.
            Random rand = new Random();

            // Fill the Grid with 25 buttons.
            for (int i = 0; i < 25; i++)
            {
                Button btn = new Button();
                btn.FontSize = 12 + rand.Next(8);
                btn.Content = "Button No. " + (i + 1);
                btn.HorizontalAlignment = HorizontalAlignment.Center;
                btn.VerticalAlignment = VerticalAlignment.Center;
                btn.Margin = new Thickness(6);
                grid.Children.Add(btn);
                Grid.SetRow(btn, i % 5);
                Grid.SetColumn(btn, i / 5);
            }

            // Size the Grid.
            grid.Measure(new Size(Double.PositiveInfinity,
                                  Double.PositiveInfinity));

            Size sizeGrid = grid.DesiredSize;

            // Determine point for centering Grid on page.
            Point ptGrid =
                new Point((dlg.PrintableAreaWidth - sizeGrid.Width) / 2,
                          (dlg.PrintableAreaHeight - sizeGrid.Height) / 2);

            // Layout pass.
            grid.Arrange(new Rect(ptGrid, sizeGrid));

            // Now print it.
            dlg.PrintVisual(grid, Title);
        }
    }

答案 1 :(得分:0)

PrintVisual打印一个Visual对象。这意味着,通过使用PrintVisual方法,我们可以打印visualtree中的任何控件,容器,窗口或用户控件。您无法打印不在visualtree中的控件