Print Grid which generated dynamically in wpf

前端 未结 2 835
日久生厌
日久生厌 2020-12-19 14:20

i want to print a grid which is generated dynamically.

Means, in the click event of the Print Button, i m generating a grid and then i want to print that grid.

相关标签:
2条回答
  • 2020-12-19 14:47

    here i get answer, from the below code i get what i want to do...

    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);
            }
        }
    
    0 讨论(0)
  • 2020-12-19 14:58

    The PrintVisual print a Visual object. That means, by using the PrintVisual method, we can print any control, container, Window or user control that is in the visualtree.You cannot print a control that is not in the visualtree

    0 讨论(0)
提交回复
热议问题