Question

I need to generate a printed form using XAML that has a header grid and a variable number of rows that can result in multiple pages as the number of rows increases. The header must appear on each page and each row may vary in height due to text wrapping within the row. I am currently trying to use ActualHeight of the ItemsControl (rows container) to determine when to generate a new page, but ActualHeight always has a value of zero.

My "XAML_Form" has the following structure. A grid is used in the ItemTemplate to allow aligning columns in the rows with columns in the header grid.

<Grid Width="980" Height="757">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid Name="_headerControl" Grid.Row="0"/>
        <ItemsControl Name=_rowsControl ItemsSource={Binding Rows} Grid.Row="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Grid/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Grid>

Our architecture has a report class that handles adding footers, page numbers, and aggregating the pages into a PDF. This report class has a nested ViewModel for each XAML View page. The ViewModel for each page uses a backing List of row objects:

   List<RowClass> RowsList;

The ViewModel also has an ICollectionView that is used to bind as the ItemsSource:

   ICollectionView Rows = new ListCollectionView(RowsList);

My report class has a CreatePages method which contains code like this:

IList<XAML_Form> pages = new List<XAML_Form>();
var vm = new PageViewModelClass();
var page = new XAML_Form { DataContext = vm };
page.InitializeComponent();
page.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity);
page.Arrange(new Rect(new Point(0,0), page.DesiredSize));
var maxRowsHeight = page.DesiredSize.Height - page._headerControl.ActualHeight;
pages.Add(page);
var rowsOnPage = 0;
foreach (var row in sourceRowsObjectList)
{
    rowsOnPage++;
    vm.RowsList.Add(row);
    vm.Rows.Refresh();
    page.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity);
    page.Arrange(new Rect(new Point(0,0), page.DesiredSize));
    if (page._rowsControl.ActualHeight <= maxRowsHeight)
        continue;

    // The rows exceed the available space; the row needs to go on the next page.
    vm.RowsList.RemoveAt(--rowsOnPage);
    vm = new PageViewModelClass();
    vm.RowsList.Add(row);
    rowsOnPage = 1;
    page = new XAML_Form { DataContext = vm };
    page.InitializeComponent();
    pages.Add(page);
}
return pages;

The initial Measure/Arrange does provide me with the expected value for maxRowsHeight. And the generated form looks fine for a single page with a few rows. My specific problem is: Why is page._rowsControl.ActualHeight always zero? And generally, is there a better approach to this problem?

Was it helpful?

Solution

Here is a solution. We are trying to separate the view concerns from the view model concerns, so there are still improvements to be made.

The CreatePages method in the report class is now:

private static IEnumerable<XAML_Form> CreatePages()
{
    IList<XAML_Form> pages = new List<XAML_Form>();
    int rowCount = sourceRowsObjectList.Count;
    int remainingRowCount = rowCount;
    do
    {
        var pageVm = new PageViewModelClass();
        var page = new XAML_Form(pageVm);
        pages.Add(page);

        int numberOfRowsToAdd = Math.Min(remainingRowCount, XAML_Form.MaxNumberOfRows);
        pageVm.AddRows(sourceRowsObjectList.Skip(rowCount - remainingRowCount).Take(numberOfRowsToAdd));
        remainingRowCount -= numberOfRowsToAdd;
        while (page.AreRowsOverflowing())
        {
            pageVm.RemoveLastRow();
            remainingRowCount++;
        }
    } while (remainingRowCount > 0);
    return pages;
}

The pertinent XAML_Form code behind is as follows:

private static int _maxNumberOfRows = -1;

public XAML_Form(PageViewModelClass viewModel)
{
    InitializeComponent();
    Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
    Arrange(new Rect(new Point(0, 0), DesiredSize);
    ViewModel = viewModel;
}

public PageViewModelClass ViewModel
{
    get { return (PageViewModelClass)DataContext; }
    private set { DataContext = value; }
}

public static int MaxNumberOfRows
{
    get     // Compute this only once, the first time it is called.
    {
        if (_maxNumberOfRows < 0) return _maxNumberOfRows;
        var page = new XAML_Form();
        var singleRowCollection = new object[] { null; }
        page._rowsControl.ItemsSource = singleItemCollection;
        page._rowsControl.UpdateLayout();
        var rowHeight = page._rowsControl.ActualHeight;
        _maxNumberOfRows = (int)((page.DesiredSize.Height - page._headerControl.ActualHeight) / rowHeight);
        page._rowsControl.ItemsSource = null;
        return _maxNumberOfRows;
    }
}

// Call this method as rarely as possible. UpdateLayout is EXPENSIVE!
public bool AreRowsOverflowing()
{
    _rowsControl.UpdateLayout();
    return _rowsControl.ActualHeight > DesiredSize.Height - _headerControl.ActualHeight;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top