0

I created a print for my UserControl. this is my ListView, this list can have data 100+

<ListView Grid.Row="2" Name="LstProduk" HorizontalContentAlignment="Stretch" Padding="0" BorderThickness="0" Margin="-1 , -0.75">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalAlignment" Value="Stretch"/>
            <Setter Property="Padding" Value="0"/>
            <Setter Property="Margin" Value="0"/>
            <Setter Property="BorderThickness" Value="0"/>
            <Setter Property="MinHeight" Value="20"></Setter>
        </Style>
    </ListView.ItemContainerStyle>
                          
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition MinHeight="20"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="37"></ColumnDefinition>
                    <ColumnDefinition Width="56"></ColumnDefinition>
                    <ColumnDefinition Width="223"></ColumnDefinition>
                    <ColumnDefinition Width="148"></ColumnDefinition>
                    <ColumnDefinition Width="148"></ColumnDefinition>
                    <ColumnDefinition Width="148"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Border Grid.Column="0" BorderThickness="0.75" BorderBrush="Black">
                    <TextBlock Text="{Binding no}" VerticalAlignment="Center" FontSize="14" Margin="5 0" TextWrapping="Wrap"></TextBlock>
                </Border>
                <Border  Grid.Column="1"  BorderThickness="0.75" BorderBrush="Black">
                    <TextBlock Text="{Binding qty}" VerticalAlignment="Center" FontSize="14" Margin="5 0" TextWrapping="Wrap"></TextBlock>
                </Border>
                <Border  Grid.Column="2"  BorderThickness="0.75" BorderBrush="Black">
                    <TextBlock Text="{Binding nama_produk}" VerticalAlignment="Center" FontSize="14" Margin="5 0" TextWrapping="Wrap"></TextBlock>
                </Border>
                <Border  Grid.Column="3"  BorderThickness="0.75" BorderBrush="Black">
                    <TextBlock Text="{Binding barcode}" VerticalAlignment="Center" FontSize="14" Margin="5 0" TextWrapping="Wrap"></TextBlock>
                </Border>
                <Border  Grid.Column="4"  BorderThickness="0.75" BorderBrush="Black">
                    <TextBlock Text="{Binding harga}" VerticalAlignment="Center" FontSize="14" Margin="5 0" TextWrapping="Wrap"></TextBlock>
                </Border>
                <Border Grid.Column="5" BorderThickness="0.75" BorderBrush="Black">
                    <TextBlock Text="{Binding total}" VerticalAlignment="Center" FontSize="14" Margin="5 0" TextWrapping="Wrap"></TextBlock>
                </Border>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

I try printing using this code:

public static void print(dynamic param , bool use_dialog = false)
{
    PrintDialog printDlg = new PrintDialog();
    if (use_dialog == true)
    {
        if (printDlg.ShowDialog() == true)
        {
            printDlg.PrintVisual(param, "Printing data.");
        }
    }
    else
    {
        printDlg.PrintVisual(param, "Printing data.");
    }
}

This is my itemsource for my ListView:

public List<Detail_transaksi_model> ListProduk = new List<Detail_transaksi_model>();
public PrintPreviewDocument()
{
    InitializeComponent();
    int no = 0;
    for (int i = 0; i < 100; i++)
    {
        ListProduk.Add(new Detail_transaksi_model
            {
                no = no,
                barcode = "",
                harga = "",
                total = "",
                nama_produk = "",
                qty = 1,
            });
        no++;
    }
    LstProduk.ItemsSource = ListProduk;
}

and I got this result enter image description here

In my picture my PrintVisual didn't create next page for my list data and cut off. How to printVisual Long Listview ?

thank you for your answer, hope you all got have a nice day !

Palle Due
  • 5,929
  • 4
  • 17
  • 32
  • Does this answer your question? [Printing large WPF User Controls](https://stackoverflow.com/questions/39660350/printing-large-wpf-user-controls) – Sinatr Nov 18 '20 at 08:20
  • i already read the question. but i cant understand to explain that example to code. can you provide me a example code? @Sinatr – Diky Ridhlo Nov 18 '20 at 08:25
  • In general you should use reports instead of screenshots, they differ in appearance and purpose (report support tables, multiple pages, headers, page counters, etc.). For making bigger screenshot you have to resize control. I haven't tried myself, but theoretically you can create a fake control (to mimic real one, but big enough), configure it and print. Maybe disconnecting real one (and connecting it back after printing?) will work too. – Sinatr Nov 18 '20 at 08:44
  • can you provide example code @Sinatr ? if you want. – Diky Ridhlo Nov 18 '20 at 08:46
  • If data are big enough you won't be able to create such screenshot anyway. I suggest you to do a [proper print preview dialog](https://stackoverflow.com/q/29892177/1997232) instead of `PrintVisual`. Personally I prefer to generate PDF (using one of libraries) and then displaying it with system default handler. – Sinatr Nov 18 '20 at 09:16

1 Answers1

1

You cannot use PrintVisual to capture records that are not currently visible on the screen. It's just like taking a screenshot. What you see is what you get.

Also, by default, any non-visibible items are virtualized away. This means that there are no visual containers for these items until you scroll them into the view. So even if you actually could capture all created containers, you still wouldn't get a picture with all items.

Instead of trying to capture the screen, you should export the source data. Here is an an example of how to do this for a DataGrid.

You may use a FlowDocument or similar to format the output.

mm8
  • 163,881
  • 10
  • 57
  • 88