Printing a ListView in UWP

Preveen 216 Reputation points

I have a UWP app that contains a ListView whose data is loaded from a SQL Express database.
I want the user to be able to print this ListView, but I can't seem to wrap my head around the print sample on the Windows Universal Samples.

The print layout should be the same as displayed in the ListView.

Can anyone point me in the right direction?
Any help is appreciated.


Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Fay Wang - MSFT 5,151 Reputation points


    Welcome to Microsoft Q&A!

    You could use the PrintHelper class to print the ListView, but it won't automatically page based on the length of the ListView, so you still need to manually paging your data in code-behind. You can divide the long ListViewand calculate the maximum number of ListView-Items that can display on the screen together and then put them into print-pages. For example:


        <DataTemplate x:Name="CustomPrintTemplate">  
                     Text="{Binding Description}"  
                     Text="{Binding SourceUrl}"  
        <Button Click="Button_Click">Print</Button>  
        <Grid x:Name="Container" Opacity="0" />  
        <ListView Height="600" x:Name="PrintSampleListView" ItemsSource="{x:Bind Lists,Mode=OneWay}" ItemTemplate="{StaticResource CustomPrintTemplate}" />  


    private PrintHelper _printHelper;  
    private async void Button_Click(object sender, RoutedEventArgs e)     
        _printHelper = new PrintHelper(Container);  
        for (int i = 0; i < Lists.Count; i = i + 4)  
            var grid = new Grid();  
            // Main content with layout from data template  
            var listView = new ListView();  
            listView.ItemTemplate = CustomPrintTemplate;  
            listView.ItemsSource = Lists.Skip(i).Take(4);  
        var printHelperOptions = new PrintHelperOptions(false);  
        printHelperOptions.Orientation = Windows.Graphics.Printing.PrintOrientation.Default;  
        await _printHelper.ShowPrintUIAsync("print sample", printHelperOptions);  

0 additional answers

Sort by: Most helpful