Search code examples
wpfvisualbrushfixeddocument

VisualBrush does "lazy evaluation"?


I create FixedDocument in more iterations (one page per iteration) like this:

PrintDialog pr = new PrintDialog();
FixedDocument doc = new FixedDocument();

foreach(var i in a)
{
    // some changes of MaingGrid here
    ...
    //
    VisualBrush vb = new VisualBrush(this.MainGrid);
    FixedPage page = new FixedPage();
    page.Width = doc.DocumentPaginator.PageSize.Width;
    page.Height = doc.DocumentPaginator.PageSize.Height;
    Rectangle rec = new Rectangle();
    rec.Width = this.MainGrid.ActualWidth;
    rec.Height = this.MainGrid.ActualHeight;
    rec.Fill = vb;
    page.Children.Add(rec);
    PageContent content = new PageContent();
    ((IAddChild)content).AddChild(page);
    doc.Pages.Add(content);
}

pr.PrintDocument(doc.DocumentPaginator, "test");

In each iteration I change the MainGrid a little. So each page should contain the actual state of MainGrid. But the printed document contains pages with same content of last iteration (in other words - the last state is on all pages in document). Is there any "lazy evaluation" of VisualBrush or something?


Solution

  • Call .Freeze() on the VisualBrush in each iteration. Otherwise, it will always be a live view of whatever visual you pointed it at.

    EDIT: Freeze doesn't work but you can render the brush into a static bitmap. See http://blog.avanadeadvisor.com/blogs/markti/archive/2008/04/14/10888.aspx