I need to create a print preview (long) using wpf user interface elements like FixedDocument, FlowDocument, PageContent, BlockUIContainer and all that. To maintain the responsiveness of my user interface, I am doing this part in a separate thread of the Thread thread (BackgroundWorker will not work, since I need an STA thread). Everything is fine up to this point.
But after the print preview is displayed, I need to print and click the "Print" icon in the generated preview, throws the infamous "The calling thread cannot access this object because it has a different thread." an exception. So is there a way?
EDIT (CODE):
Dispatcher.CurrentDispatcher.Invoke(new Action(() =>
{
Thread thread = new Thread(() =>
{
FixedDocument document = renderFlowDocumentTemplate(report);
PrintPreview preview = new PrintPreview();
preview.WindowState = WindowState.Normal;
preview.documentViewer.Document = document;
preview.ShowDialog();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}));`
, RenderFlowDocumentTemplate() ( ) . PrintPreview - , DocumentViewer, , "", "", "PrintDialog".
EDIT (XAML):
<cw:CustomWindow x:Class="MyApp.Reports.PrintPreview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cw="clr-namespace:MyApp.UI.CustomWindows;assembly=MyApp.UI.CustomWindows">
<DocumentViewer Margin="0,30,0,0" Name="documentViewer"></DocumentViewer>
</cw:CustomWindow>`