2011-11-23 73 views
0

我的silverlight應用程序有一個用戶控件(稱爲CertificatePrintControl),用於顯示和打印一些信息。 我想在另一個控件內部對printCtl進行分類並在運行時打印內容。 的例子是:打印運行時usercontrol silverlight 4

foreach (var certId in CertToPrint) 
    { 
     var certPrintController = new CertificatePrintControl() { PrintDocument = pd, CertGuid = certId }; 
     certPrintController.Print(); 
     certPrintController = null; 
    } 

的問題是,CertificatePrintControl裏面有這個代碼

void pd_PrintPage(object sender, PrintPageEventArgs e) 
    { 
     try 
     { 
      var oldW = lstMainPrintCertificate.Width; 
      var oldH = lstMainPrintCertificate.Height; 
      lstMainPrintCertificate.Width = e.PrintableArea.Width; 
      lstMainPrintCertificate.Height = e.PrintableArea.Height; 
      e.PageVisual = lstMainPrintCertificate; 
      lstMainPrintCertificate.Width = oldW; 
      lstMainPrintCertificate.Height = oldH; 
     } 
     catch (Exception ex) 
     { 
      // 
     } 
    } 

,讓

{System.InvalidOperationException: Element is already the child of another element. 
    at MS.Internal.XcpImports.CheckHResult(UInt32 hr) 
    at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh) 
    at System.Windows.Printing.PrintPageEventArgs.set_PageVisual(UIElement value) 
    at com.breathesaftey.Control.CertificatePrintControl.pd_PrintPage(Object sender, PrintPageEventArgs e)} 

我可以明白爲什麼,但我找不到解決方法。

感謝

回答