Layout measurement override of element should not return NaN values as its DesiredSize
I've run into a similar issue when I try to call Measure
and pass the dynamically calculated size to that method:
Layout measurement override of element 'System.Windows.Controls.StackPanel' should not return NaN values as its DesiredSize.
I am trying to create a StackPanel on the fly and print it. Here's 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);
/开发者_JAVA技巧/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");
Any clue what could be the cause of this? And the strange thing is this happens only with StackPanel
. If I try to create a simple TextBlock
for testing purpose and try to print that, everything works fine with no errors. I was wondering what's different when we call Measure
for StackPanel
?
Any help to fix this issue would be really great!
Your are probably passing in Size(double.NaN, double.NaN) to the Measure call, which is bad. Your printPanel.ActualWidth/ActualHeight should be 0.0, which results in NaN. This is how Double defines NaN:
public const double NaN = (double) 0.0 / (double) 0.0;
So you would need to pass in Size(double.PositiveInfinity, double.PositiveInfinity) to get the desired size. After you arrange the printPanel, the ActualWidth/ActualHeight should be valid.
精彩评论