title: Printing dynamically created WindowsFormsHost in WPF
body: <p>I'm trying to print a WindowsFormsHost in a WPF application.
  It prints ok, if the WindowsFormsHost object is created by xaml, but it prints an empty page if I create WindowsFormsHost in code.</p>
  
  <p><strong>How do I have to create WindowsFormsHost object in code in order to get it to printout?</strong></p>
  
  <p>Here is the simplified but working code:</p>
  
  <pre><code>            PrintDialog dialog = new PrintDialog();
              if (dialog.ShowDialog() == true)
              {
                  System.Windows.Forms.TextBox ctrl = new System.Windows.Forms.TextBox();
                  ctrl.Text = "Test";
                  ctrl.BackColor = System.Drawing.Color.Red;
  #if RUNTIME
                  WindowsFormsHost wfh = new WindowsFormsHost();
                  wfh.Child = ctrl;
                  FrameworkElement visual = wfh;
  #else
                  WFHostTest.Child = ctrl;
                  FrameworkElement visual = WFHostTest;
  #endif
  
                  visual.Margin = new Thickness(0, 0, 0, 0);
                  visual.Measure(new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight));   
                  visual.Arrange(new Rect(0, 0, dialog.PrintableAreaWidth, dialog.PrintableAreaHeight)); 
                  visual.UpdateLayout();
                  dialog.PrintVisual(visual, "Test");
              }
  </code></pre>
  
  <p>This code prints the TextBox only when RUNTIME is not defined.
  If RUNTIME is defined it prints an empty page.
  WFHostTest is defined in xaml as</p>
  
  <pre><code>&lt;WindowsFormsHost Grid.Column="1" Grid.Row="2" Name="WFHostTest" /&gt;
  </code></pre>