0

In my Application, I have the need for generating different reports. Most of them fit on a single page. I created those reports with FixedDocuments. Now I try to create some kind of letter in a FixedDocument. It contains among other things a header, a complimentary close and a subject. These parts work without any problems. They are all separated into UserControls.

The main content of the letter gives me some headache. This should be a nested ItemsControl bound to a custom list (categoryList). Each item of the custom list consists of a string (category) and another list (valueList). The elements of the other list consist of two strings (caption, value). The ItemsControl looks like this:

<ItemsControl ItemsSource="{Binding categoryList}"
              DockPanel.Dock="Top"
          Margin="20,10,0,0">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical" Margin="0" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding category}"/>
                <ItemsControl ItemsSource="{Binding valueList}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" Margin="0" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition />
                                    <ColumnDefinition />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding caption}"
                                                       Grid.Column="0" />
                                <TextBlock Text="{Binding value}"
                                                       Grid.Column="1" />
                            </Grid>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

If the categoryList and the valueList both contain only a few elements, everything works fine. But with a certain amount of elements, the ItemsControl gets clipped.

This is how I create the FixedDocument via code:

FixedDocument doc = new FixedDocument();

FixedPage page = new FixedPage();
PageContent page1= new PageContent();

//All UserControls are placed inside a DockPanel
DockPanel panel = new DockPanel();

//UserControl with header
Header header = new Header();
DockPanel.SetDock(header, Dock.Top);
panel.Children.Add(header);

//UserControl with complimentary close
Complimentary complimentary = new Complimentary();
DockPanel.SetDock(complimentary, Dock.Top);
panel.Children.Add(complimentary);

//UserControl with subject
Subject subject = new Subject();
DockPanel.SetDock(subject , Dock.Top);
panel.Children.Add(subject);

//UserControl with ItemsControl for categoryList
Categories categories = new Categories();
DockPanel.SetDock(categories,Dock.Top);
panel.Children.Add(categories);

//Add the DockPanel to the page
page.Children.Add(panel);

//Set the PageContent
page1.Child = page;
doc.Pages.Add(page1);
//Set the DataContext for the Binding
doc.DataContext = this.listWithValues;
//Display the result in a DocumentReader
this.reader.Document = doc;

Is there a way to place a page break inside of the ItemsControl? Either before the first category that will cause the overflow. Or even better inside the category that will cause the overflow.

Thanks for any suggestion and advice! If any other information is needed, feel free to ask.

user2219063
  • 128
  • 6

1 Answers1

1

I've been looking into the FixedDocument myself as a solution to enable copying from a statistics display.

The FixedDocument class is, by definition, WYSIWYG, meaning that the content on each page is explicitly defined; you define precisely which content you want on a page and then add that PageContent to the FixedDocument. By design, content will not flow from page to page.

Since, as you say, your categories are "overflowing," you were already on the way to the solution...you want to use a FlowDocument instead of a FixedDocument. A FlowDocument will flow your content based on the content itself, as well as the font size and other context.

karfus
  • 869
  • 1
  • 13
  • 20
  • karfus, thanks for your answer! I also thought about using FlowDocument. It's quite some time ago, since I posted the question, but I can remember that there where some points why I could not use FlowDocuments. I am now building up the single pages in the FixedDocument on the fly by code. It's a bit more work, but I can define my own rules, when to make a page break. – user2219063 Nov 20 '14 at 08:11