title: My Print PDF Doesn't Show the Binded Data of the Original Window - C# MVVM
body: <p>I am trying to print my window (Print Preview) using MVVM architecture. So far I was able to get the controls of the window into the print. But it doesn't show the data bindings. </p>
  
  <p>This is my PrintPreview.xaml code</p>
  
  <pre><code>&lt;ScrollViewer VerticalScrollBarVisibility="Auto"&gt;
      &lt;!--Main Grid--&gt;
      &lt;Grid x:Name="MainGrid"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="2.5*"/&gt;
              &lt;ColumnDefinition Width="3*"/&gt;
              &lt;ColumnDefinition Width="2.5*"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
  
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height ="1.5*"/&gt;
              &lt;RowDefinition Height ="1.7*"/&gt;
              &lt;RowDefinition Height ="5*" MinHeight="200"/&gt;
              &lt;RowDefinition Height ="7*" MinHeight="200"/&gt;
              &lt;RowDefinition Height ="6*" MinHeight="100"/&gt;
              &lt;RowDefinition Height ="2*"/&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Grid Grid.RowSpan="2" Grid.ColumnSpan="3" Background="#9dc6a7"/&gt;
  
          &lt;!--Patient Id block--&gt;
          &lt;StackPanel Grid.ColumnSpan="2" Style="{StaticResource StackpanelStyle}"&gt;
              &lt;Label FontSize="16" FontWeight="Bold" Foreground="White"&gt;
                  Patient ID :
              &lt;/Label&gt;
              &lt;Label FontSize="16" Foreground="White" Margin="5,0,0,0" Content="{Binding Path=PatientID}"/&gt; 
          &lt;/StackPanel&gt;
  
          &lt;!--Date block--&gt;
          &lt;StackPanel Grid.ColumnSpan="3" Style="{StaticResource StackpanelStyle}" HorizontalAlignment="Right"&gt;
              &lt;Label FontSize="16" FontWeight="Bold" Foreground="White"&gt;
                  Date :
              &lt;/Label&gt;
              &lt;Label Name="date_time" FontSize="16" Foreground="White" Margin="5,0" Content="{Binding Path=CurrentDate}"/&gt;
          &lt;/StackPanel&gt;
  
          &lt;!--Title block--&gt;
          &lt;TextBlock Grid.Row="1" Grid.ColumnSpan="3"  HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" FontWeight="Bold" TextDecorations="Underline" Foreground="White"&gt;
              Patient Registration Information
          &lt;/TextBlock&gt;
  
          &lt;!--Sub grid 1--&gt;
          &lt;Grid Grid.Row="2" Grid.ColumnSpan="3" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="25,10,25,10"&gt;
  
              &lt;!--User's image View and name--&gt;
              &lt;StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Vertical"&gt;
                  &lt;Ellipse Width="150" Height="150"&gt;
                      &lt;Ellipse.Fill&gt;
                          &lt;ImageBrush x:Name="ImageViewer2" ImageSource="{Binding Path=PatientImageSource}"/&gt;
                      &lt;/Ellipse.Fill&gt;
                  &lt;/Ellipse&gt;
                  &lt;StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Margin="0,10"&gt;
                      &lt;Label Style="{StaticResource LabelStyles}" Margin="0"&gt;Patient Name :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewName" Style="{StaticResource TextBlockStyle}" Text="{Binding Path = PatientName}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/StackPanel&gt;
          &lt;/Grid&gt;
  
          &lt;!--Sub grid 2--&gt;
          &lt;GroupBox Grid.Row="3" Grid.ColumnSpan="3" HorizontalAlignment="Stretch"  VerticalAlignment="Center" Margin="25,-35,25,0"&gt;
              &lt;GroupBox.Header&gt;
                  &lt;TextBlock Style="{StaticResource TextBlockStyleControl}"&gt;Patient Personal Information&lt;/TextBlock&gt;
              &lt;/GroupBox.Header&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="500*"/&gt;
                      &lt;ColumnDefinition Width="500*"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
  
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="5*"/&gt;
                      &lt;RowDefinition Height="3*" MaxHeight="80"/&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
                  &lt;StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical" Margin="10,5"&gt;
                      &lt;Label Style="{StaticResource PreviewLabelStyles}" &gt;Address :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewANum"  Style="{StaticResource TextBlockStyle}" Text="{Binding Path =  PatientAddress.Number}"/&gt;
                      &lt;TextBlock x:Name="ViewAStrt" Style="{StaticResource TextBlockStyle}" Text="{Binding Path =  PatientAddress.Street}"/&gt;
                      &lt;TextBlock x:Name="ViewACity" Style="{StaticResource TextBlockStyle}" Text="{Binding Path =  PatientAddress.City}"/&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="50,5,10,10"&gt;
                      &lt;Label  Style="{StaticResource PreviewLabelStyles}"&gt;Gender :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewGender" Style="{StaticResource TextBlockStyle}" Text="{Binding Path = PatientGender}"/&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" Margin="10,5"&gt;
                      &lt;Label  Style="{StaticResource PreviewLabelStyles}"&gt;Date of Birth :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewDob" Style="{StaticResource TextBlockStyle}" Text="{Binding Path = PatientDateOfBirth}"/&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;StackPanel Grid.Row="1" Grid.Column="1" Orientation="Vertical" Margin="50,5,10,5"&gt;
                      &lt;Label Style="{StaticResource PreviewLabelStyles}"&gt;Age :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewAge" Style="{StaticResource TextBlockStyle}" Text="{Binding Path = PatientAge}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
          &lt;/GroupBox&gt;
  
          &lt;!--Sub grid 3--&gt;
          &lt;GroupBox Grid.Row="4" Grid.ColumnSpan="3" HorizontalAlignment="Stretch"  VerticalAlignment="Center" Margin="25,-25,25,0"&gt;
              &lt;GroupBox.Header&gt;
                  &lt;TextBlock Style="{StaticResource TextBlockStyleControl}"&gt;Patient Medical Information&lt;/TextBlock&gt;
              &lt;/GroupBox.Header&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="500*"/&gt;
                      &lt;ColumnDefinition Width="500*"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
  
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="3*"/&gt;
                      &lt;RowDefinition Height="3*" /&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
                  &lt;StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical" Margin="10,5"&gt;
                      &lt;Label Style="{StaticResource PreviewLabelStyles}"&gt;Department :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewDepartment" Style="{StaticResource TextBlockStyle}" Text="{Binding Path=PatientDepartment}"/&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical" Margin="50,5,10,0"&gt;
                      &lt;Label  Style="{StaticResource PreviewLabelStyles}"&gt;Ward :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewWard" Style="{StaticResource TextBlockStyle}" Text="{Binding Path=PatientWard}"/&gt;
                  &lt;/StackPanel&gt;
  
                  &lt;StackPanel Grid.Row="1" Grid.Column="0" Orientation="Vertical" Margin="10,5,10,10"&gt;
                      &lt;Label  Style="{StaticResource PreviewLabelStyles}"&gt;Doctor in Charge :&lt;/Label&gt;
                      &lt;TextBlock x:Name="ViewDoc" Style="{StaticResource TextBlockStyle}" Text="{Binding Path=PatientDotorcInCharge}"/&gt;
                  &lt;/StackPanel&gt;
              &lt;/Grid&gt;
          &lt;/GroupBox&gt;
  
          &lt;Button Grid.Row="5" Grid.Column="2" Style="{StaticResource ButtonStyle}" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="20,7,26,0" Content="Print Content" Command="{Binding Path=PrintCommand}"/&gt;
  
      &lt;/Grid&gt;
  &lt;/ScrollViewer&gt;
  </code></pre>
  
  <p>This is my PrintPreview ViewModel code,</p>
  
  <pre><code>class PrintPreviewViewModel
  {
      #region Data Members
      private IWindowService m_windowService;
      #endregion
  
      #region Constructors
      public PrintPreviewViewModel(PatientRecordDetailsModel patient)
      {
          string id = patient.PatientId;
  
          PatientID = id.PadRight(6).Substring(0, 6);
          PatientName = patient.PatientName;
          PatientAddress = patient.PatientAddress;
          PatientGender = patient.PatientGender;
          PatientDateOfBirth = patient.PatientDateOfBirth.ToShortDateString();
          PatientAge = patient.PatientAge;
          PatientImageSource = patient.PatientImageSource;
          PatientDepartment = patient.PatientDepartment;
          PatientWard = patient.PatientWard;
          PatientDotorcInCharge = patient.PatientDoctorInCharge;
  
          m_windowService = new WindowService();
      }
      #endregion
  
      #region Properties
      public string PatientID { get; set; }
  
      public string PatientName { get; set; }
  
      public PatientAddress PatientAddress { get; set; }
  
      public string PatientGender { get; set; }
  
      public string PatientDateOfBirth { get; set; }
  
      public int PatientAge { get; set; }
  
      public ImageSource PatientImageSource { get; set; }
  
      public string PatientDepartment { get; set; }
  
      public string PatientWard { get; set; }
  
      public string PatientDotorcInCharge { get; set; }
      #endregion
  
      #region &lt;PrintPreview&gt; Members
      public string CurrentDate =&gt; m_windowService.GetCurrentDate();
      #endregion
  
      #region Properties : Commands
      public ICommand PrintCommand
      {
          get =&gt; new PatientRecordDetailsCommands(param =&gt; OnPrintCommandExecute(), param =&gt; OnPrintCommandCanExecute());
      }
      #endregion
  
      #region Handlers : Commands
      private void OnPrintCommandExecute()
      {
          m_windowService.PrintWindow();
      }
  
      private bool OnPrintCommandCanExecute()
      {
          return true;
      }
      #endregion
  
  }
  }
  </code></pre>
  
  <p>As you can see I am opening the printPreview Window from another window through a service class. The patient object is being send through that. Also the implementation of print button command is also defined in the same service class. I used this approach to reduce the MVVM violations. 
  This is my Service class</p>
  
  <pre><code>   class WindowService : IWindowService
     {
  
      public void CreateWindow(PatientRecordDetailsModel patient)
      {
          PrintPreview printPreview = new PrintPreview();
          printPreview.DataContext = new PrintPreviewViewModel(patient);
          printPreview.Show();
      }
  
      public string GetCurrentDate()
      {
          DateTime dateTime = DateTime.Now;
  
          string date = dateTime.ToString("d");
          string time = dateTime.ToString("T");
          string Date_Time = date + " " + time;
  
          return Date_Time;
      }
  
      public void PrintWindow()
      {
          PrintPreview printPreview = new PrintPreview();
          PrintDialog printDlg = new PrintDialog();            
          PrintQueue ReceiptPrinter = new LocalPrintServer().GetPrintQueue("Microsoft Print To PDF");
          PrintCapabilities PC = ReceiptPrinter.GetPrintCapabilities();
          printDlg.PrintQueue = ReceiptPrinter;
          printPreview.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
          Size PreGridSize = printPreview.DesiredSize;
          double Scale = Math.Min(PC.PageImageableArea.ExtentWidth / PreGridSize.Width, PC.PageImageableArea.ExtentHeight / PreGridSize.Height);
          printPreview.LayoutTransform = new System.Windows.Media.ScaleTransform(Scale, Scale);
          Size PaperSize = new Size(PC.PageImageableArea.ExtentWidth, Math.Min(PC.PageImageableArea.ExtentHeight, PreGridSize.Height));
          printPreview.Measure(PaperSize);
          Point ptGrid = new Point(PC.PageImageableArea.OriginWidth, PC.PageImageableArea.OriginHeight);
          printPreview.Arrange(new Rect(ptGrid, PaperSize));
  
          Application.Current.Dispatcher.BeginInvoke(
              new Action(delegate () { printDlg.PrintVisual(printPreview.MainGrid, "PrintPreview"); }),
              DispatcherPriority.Send);
  
      }
  }
  </code></pre>
  
  <p>I tried assigning the patient object to the datacontext inside the PrintWindow(), but it returns a null object. </p>
  
  <p><a href="https://i.stack.imgur.com/Av7sU.png" rel="nofollow noreferrer">This is the original printPreview Window</a></p>
  
  <p><a href="https://i.stack.imgur.com/mx9tG.png" rel="nofollow noreferrer">This is how the print looks like</a></p>
  
  <p>So I guess there should be a proper way of doing this.</p>