Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:18:37+00:00 2026-06-17T20:18:37+00:00

I need to generate a printed form using XAML that has a header grid

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:18:38+00:00Added an answer on June 17, 2026 at 8:18 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to generate report that will show number of new / changed rows
I need to generate some token that can only take on a range of
I need generate action links outside controllers. I can use Html.Action in Views, Url.Action
I'm using an NSTextField to display lines of text that will be printed on
I need to generate a bunch of tables each with a header( <thead> made
I'm using php to generate an html page that displays blog/thread items, and I
I need to generate a probability histogram of the number of rolls before a
I need to generate a couple of objects from lists in Javascript. In Python,
I need to generate such pattern for my input, for example: +375 29 911-91-91
I need to generate css based on client queries. I use the @import url()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.