Sunday, January 29, 2012

WPF Printing using FixedDcoument


Print Preview Popup as FixedDocument

(1) PageContent will has Compiler Error but can stil render with FixedPage---Known Defect of WPF.
(2) Fixed document will has toolbar shown by WPF, no coded needed. So this is simplest printing

<Window x:Class="TestWPFPrinting.PrintPreview"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PrintPreview" Height="300" Width="300">
    <FixedDocument Name="customerReport">

        <PageContent>

            <FixedPage>
                <Label FontSize="20" Margin="100,20,0,0">REPORT</Label>
                <ListView  BorderThickness="0" Margin="50,100,0,0" FontSize="14" Width="Auto" Height="Auto" ItemsSource="{Binding}">
                    <ListView.View>
                        <GridView x:Name="gridReport">

                            <GridViewColumn Width="200" Header="FirstName" DisplayMemberBinding="{Binding Path=FirstName}">

                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Label/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Width="200" Header="LastName" DisplayMemberBinding="{Binding Path=LastName}">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <Label/>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                        </GridView>
                    </ListView.View>
                </ListView>

            </FixedPage>

        </PageContent>
    </FixedDocument>
</Window>

Bind to Data
   public partial class PrintPreview : Window
    {
        private List<Customer> _customers;
        public PrintPreview(List<Customer> customers) 
        {
            InitializeComponent();
            _customers = customers;
            // generate report 
            this.DataContext = _customers; 
        }
    }

    public class Customer
    {
        private string _firstName;
        private string _lastName;

        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    }

Main Form
       private void button1_Click(object sender, RoutedEventArgs e)
        {

            List<Customer> customers = new List<Customer>();

            for (int i = 1; i <= 200; i++)
            {
                Customer customer = new Customer();
                customer.FirstName = "FirstName " + i;
                customer.LastName = "LastName " + i;
                customers.Add(customer);
            }
            PrintPreview w = new PrintPreview(customers);
            w.Show();


No comments: