Wpf how to print ListBox

2019-05-22 22:53发布

问题:

I would like to know what is the easiest way to print ListBox's values. I have tried to use FlowDocumentReader but with no success.

回答1:

If you are trying to print a visual element,you can use

             PrintDialog printDlg = new PrintDialog();
             printDlg.PrintVisual(ListBox1, "Listbox Printing.");

It can be used to print any visual object(any control, container, Window or user control)

If you are looking to print the items only then you can use the FlowDocument

             FlowDocument fd = new FlowDocument();
             foreach (object item in items)
             {
                 fd.Blocks.Add(new Paragraph(new Run(item.ToString())));
             }
             fd.Print();

or

             PrintDialog pd = new PrintDialog();
             pd.PrintDocument(fd);


标签: wpf listbox