Formally known as GridViewGuy
Videos | Podcasts

Creating FixedDocument in WPF Using Custom Collection
AzamSharp
Published Date: 11/18/2008 11:55:36 AM
Views: 791

Abstract:
Windows Presentation Foundation includes FixedDocument and FlowDocument which are used to display the documents to the user in a nice manner. Mainly FixedDocument and FlowDocuments are used to display XPS files. In this article we will take a different route and create a FixedDocument using a custom collection.

Introduction:

Windows Presentation Foundation includes FixedDocument and FlowDocument which are used to display the documents to the user in a nice manner. Mainly FixedDocument and FlowDocuments are used to display XPS files. In this article we will take a different route and create a FixedDocument using a custom collection.

Creating a Customer Entity:

Our first task is to create a Customer entity object. Take a look at the Customer classimplementation below:

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; }
}
}


The Customer object consists of the FirstName and the LastName. Now, let's see how to create a custom collection of Customer objects and populate it.

Populating Customer Collection:

The following code will populate the Customer collection with some dummy data.

List customers = new List();

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



Now let's see the PrintSummary page which is responsible for displaying the Customer collection in a nice manner.

Here is the code behind:

public partial class PrintSummary : System.Windows.Window
{
private List _customers;

public PrintSummary()
{
InitializeComponent();
}

public PrintSummary(List customers) : this()
{
_customers = customers;
// generate report
this.DataContext = _customers;
}

}


And here is the XAML part of the application.

      <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>

The FixedDocument contains the PageContent element which contains the FixedPage element. The ListView contains the GridView control which is used to display the collection in a well formatted way. If you try to switch to the design mode you will get the following error:

Property 'Pages' does not support values of type 'PageContent'.

Don't worry this is a BUG in Visual Studio WPF designer. It is documented here. Hopefully, it will be fixed in Visual Studio 2010 release.

Now, when you run it you will get the following result:



Conclusion:

FixedDocument is mainly designed to support XPS formatted pages but you can also make it work when displaying custom collections. In the future articles we will look at the FlowDocument which is used to display multiple pages.

[Download Sample]


Similar Articles

Creating a Simple RSS Reader Using Windows Presentation Foundation

Creating WPF TextBox UserControl to Perform Custom Validation

Creating Master Pages in Windows Presentation Foundation

DataBinding Custom Properties Using User Controls in WPF

Introduction to TabControl and Scrollable Tabs


Product Spotlight Hosting Spotlight

Enter Comment/Feedback

 
 
 
 
 

Comments/Feedbacks