GorillaApe
GorillaApe

Reputation: 3641

Printing Visual with WPF

I have a paginator for visuals. The problem is that when i create the visual from code i have to call Measure and Arrange. But i want to give them infinity value.

  stackpanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  stackpanel.Arrange(new Rect(new Size(double.PositiveInfinity, double.PositiveInfinity)));

This causes

Cannot call Arrange on a UIElement with infinite size or NaN. Parent of type 'System.Windows.Controls.StackPanel' invokes the UIElement. Arrange called on element of type 'System.Windows.Controls.StackPanel'.

Upvotes: 0

Views: 1133

Answers (1)

Klaus78
Klaus78

Reputation: 11906

In the Measure step, a parent asks its children how much space they desire given a certain amount of space. This amount of space can also be infinity, which would mean to ask the children 'how much space would you like? There is no limit.'

In the Arrange step the elements are physically arranged on the layout. A parent tells its children where they are arranged and how much space they get. However the size parameter of the Arrange function cannot be infinity because this is the actual size the element will have.

Do you use Infinity because you want the visual occupy all the available space on printed page? If yes what I usually do is that

Size sz = new Size(printCapabilities.PageImageableArea.ExtentWidth, 
             printCapabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.  
yourElement.Measure(sz);
yourElement.Arrange(new Rect(new Point(printCapabilities.PageImageableArea.OriginWidth, printCapabilities.PageImageableArea.OriginHeight), sz));

Upvotes: 3

Related Questions