I am trying to setup a printable view of my WPF window and I'm having a very hard time getting the content to size properly (surprise!).
Specifically, Measure doesn't seem to be setting the DesiredSize property of my window.
The basic approach I'm trying to take is that I've created a separate Window object (PrintView) that contains the controls I wish to print. I'm now programmatically attempting to instantiate the Window and then send it to the printer.
PrintView printView = new PrintView(m_Model.Clone() as MyModel);
printView.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
printView.Arrange(new Rect(new Point(0, 0), printView.DesiredSize));
thePrintDialog.PrintVisual(printView, "Strategy");//blank page every time
Unfortunately, printView.DesiredSize on line 3 there is always 0,0 after a call to .Measure, which causes the printed page to be blank, of course. I thought that .Measure is supposed to set that property. Interestingly, if stick a call to .ShowDialog() in there instead of the Measure & Arrange, it works properly, so as I understand it, that means the issue is that the layout pass has not occured. I just can't figure out how to force it to occur.
Any help would be greatly appreciated.
EDIT: POSTING CODE FOR PrintView
public PrintView(MyModel p_Model)
{
InitializeComponent();
TabSetContent1.Initialize(p_Model, p_Model, new Model.Workbook());
TabCompareContent1.Initialize(p_Model, new Model.Workbook());
}
And the XAML:
<Window x:Class="Cmi.Analytics.DecisionPathway.Ui.UserControls.Printing.PrintView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:components="clr-namespace:MyNamespace">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<components:TabSetContent x:Name="TabSetContent1" Grid.Row="0" IsPrintView="true" />
<components:TabCompareContent x:Name="TabCompareContent1" Grid.Row="1" IsPrintView="true" />
</Grid>
</Window>
The calls to Initialize for each of the usercontrols kick off lots of stuff for binding, etc... There's quite a bit of code involved. These exact same controls are used on the main.xaml page with the same calls to .Initialize() and everything works as expected.