Overriding element layout overrides should not return NaN values ​​as required size

I had a similar problem when I try to call Measureand pass dynamically calculated size to this method:

Completing the replacement of the layout of the System.Windows.Controls.StackPanel element should not return NaN values ​​as DesiredSize.

I am trying to create a StackPanel on the fly and print it. Here is my code:

StackPanel printPanel = new StackPanel();

PrintableArea.Children.Remove(ChartBorder);
printPanel.Children.Add(ChartBorder);

//Get selected printer capabilities
System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);

//Get scale of the print wrt to screen of visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / printPanel.ActualWidth, capabilities.PageImageableArea.ExtentHeight / printPanel.ActualHeight);

//Transform the Visual to scale
printPanel.LayoutTransform = new ScaleTransform(scale, scale);

//Get the size of the printer page
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

//Update the layout of the visual to the printer page size.
printPanel.Measure(sz);
printPanel.Arrange(new Rect(new Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

dialog.PrintVisual(printPanel, "Test");

Is there any clue what could be causing this? And it’s strange that this only happens with StackPanel. If I try to create a simple one TextBlockfor testing purposes and try to print it, everything will work fine without errors. I was wondering what else when we call Measurefor StackPanel?

!

+3
1

, Size (double.NaN, double.NaN) Measure, . printPanel.ActualWidth/ActualHeight 0.0, NaN. Double NaN:

   public const double NaN = (double) 0.0 / (double) 0.0;

, Size (double.PositiveInfinity, double.PositiveInfinity), . , printPanel, ActualWidth/ActualHeight.

+2

All Articles