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);
System.Printing.PrintCapabilities capabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket);
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / printPanel.ActualWidth, capabilities.PageImageableArea.ExtentHeight / printPanel.ActualHeight);
printPanel.LayoutTransform = new ScaleTransform(scale, scale);
Size sz = new Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
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?
!