UWP打印需要扩展到多个页面的Long Datagrid

时间:2019-05-25 13:18:42

标签: c# xaml uwp

我正在使用(uwp,c#,MySQL)制作会计程序,我想知道在打印报告时,如果datagrid很长,如何将其余部分扩展到新页面,以便所有报告都可以打印在多个页面上。

我使用了打印示例https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing中的printhelp.cs,但它仅将数据网格打印在一页上,而没有将其余部分扩展到更多页。

UWP XAML:

<Grid>
    <controls:DataGrid Grid.Row="2" Grid.RowSpan="5" Grid.ColumnSpan="2"
        x:Name="TextContent"
        Foreground="Black"
        Background="White"
        ItemsSource="{x:Bind SelectSOA()}"
        AutoGenerateColumns="False"    
        GridLinesVisibility="Horizontal">



        <controls:DataGrid.Columns>
            <controls:DataGridTextColumn   Header="ID" Binding="{Binding ID}" />
            <controls:DataGridTextColumn Header="Account" Binding="{Binding Principal}" />
            <controls:DataGridTextColumn Header="Balance" Binding="{Binding Client}" />
        </controls:DataGrid.Columns>

    <RichTextBlockOverflow  x:Name="FirstLinkedContainer" OverflowContentTarget="{Binding ElementName=ContinuationPageLinkedContainer}" Grid.Row="2" Grid.Column="0"/>
    <RichTextBlockOverflow x:Name="ContinuationPageLinkedContainer" Grid.Row="3" Grid.ColumnSpan="2"/>


</Grid>

我希望在打印时,Datagrid是否很长才能扩展到下一页。但它只会打印第一页,而不会将其余页面扩展到新页面。

1 个答案:

答案 0 :(得分:0)

您可以使用Go Playground类来打印DataGrid,但仍然需要在代码隐藏中手动分页数据。

请参阅Print Helper,以了解如何使用此类打印XAML控件。

根据该示例,我做了一些更改,以制作一个简单的代码示例供您参考:

<Grid>
    <Grid x:Name="RootGrid"
          HorizontalAlignment="Center"
          VerticalAlignment="Center">
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid x:Name="Container"
              Grid.RowSpan="2"
              Opacity="0" />
        <Grid x:Name="CustomPrintContainer"
              Opacity="0" />
        <Grid x:Name="DirectPrintContainer">
            <Grid x:Name="PrintableContent">
                <Grid x:Name="XamlRoot" />
            </Grid>
        </Grid>
    </Grid>
    <Button Content="Print" Click="Button_Click" VerticalAlignment="Bottom"></Button>
</Grid>
public List<Person> Persons { get; set; }

    public MainPage()
    {
        this.InitializeComponent();

        Persons = new List<Person>();
        for (int i = 0; i < 100; i++)
        {
            Persons.Add(new Person
            {
                PersonId = i,
                FirstName = "FirstName" + i,
                LastName = "LastName" + i,
                Position = "Network Administrator " + i
            });
        }
    }

    private PrintHelper _printHelper;

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        _printHelper = new PrintHelper(CustomPrintContainer);

        var pageNumber = 0;

        for (int i = 0; i < Persons.Count; i = i + 10)
        {
            var grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
            grid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

            // Static header
            var header = new TextBlock { Text = "Custom Print", Margin = new Thickness(0, 0, 0, 20) };
            Grid.SetRow(header, 0);
            grid.Children.Add(header);

            // Main content with layout from data template
            var dataGrid = new DataGrid();
            dataGrid.AutoGenerateColumns = true;
            dataGrid.ItemsSource = Persons.Skip(i).Take(10);
            Grid.SetRow(dataGrid, 1);
            grid.Children.Add(dataGrid);

            // Footer with page number
            pageNumber++;
            var footer = new TextBlock { Text = string.Format("page {0}", pageNumber), Margin = new Thickness(0, 20, 0, 0) };
            Grid.SetRow(footer, 2);
            grid.Children.Add(footer);

            _printHelper.AddFrameworkElementToPrint(grid);
        }

        _printHelper.OnPrintCanceled += _printHelper_OnPrintCanceled;
        _printHelper.OnPrintFailed += _printHelper_OnPrintFailed;
        _printHelper.OnPrintSucceeded += _printHelper_OnPrintSucceeded;

        var printHelperOptions = new PrintHelperOptions(false);
        printHelperOptions.Orientation = Windows.Graphics.Printing.PrintOrientation.Default;
        printHelperOptions.AddDisplayOption(StandardPrintTaskOptions.Orientation);
        await _printHelper.ShowPrintUIAsync("print sample", printHelperOptions);
    }
public class Person
{
    public int PersonId { get; set; }
    public int DepartmentId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Position { get; set; }
}

Windows Community Toolkit PrintHelper sample