1

I am running the following code in a background thread as an STA apartment to provide a Print Preview in a document viewer:

 // Print Preview
        public static void PrintPreview(FixedDocument fixeddocument)
        {
            MemoryStream ms = new MemoryStream();

            using (Package p = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite))
            {
                Uri u = new Uri("pack://TemporaryPackageUri.xps");

                PackageStore.AddPackage(u, p);

                XpsDocument doc = new XpsDocument(p, CompressionOption.Maximum, u.AbsoluteUri);

                XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

                writer.Write(fixeddocument.DocumentPaginator);

                var previewWindow = new Window();
                var docViewer = new DocumentViewer();
                previewWindow.Content = docViewer;

    THIS FAILS --->   docViewer.CommandBindings.Remove(???Print Button???);

                FixedDocumentSequence fixedDocumentSequence = doc.GetFixedDocumentSequence();
                docViewer.Document = fixedDocumentSequence as IDocumentPaginatorSource;

                previewWindow.ShowDialog();

                PackageStore.RemovePackage(u);
                doc.Close();
            }
        }

All works well. However, since this is running in its own thread--not the main thread--the print dialogue on the Document Viewer crashes.

In code, how can I remove and/or disable the Print button from the DocumentViewer?? (I have read everything I could find in Google, and it all is in XAML, not very helpful).

Any help is much appreciated. TIA

Update#1: The only way I can see to do this, is to drop the Print Button from the control template and use a custom Document Viewer. A workable style is given at Document Viewer Style.

It still would be nice if I could simply remove the button from the system Document viewer?

Alan Wayne
  • 5,122
  • 10
  • 52
  • 95

1 Answers1

0

Using the method from this answer, you can alter the visibility of the PrintButton programmatically like this. Let's say I put the method in a class called UIElementHelper:

     var button = UIElementHelper.FindChild<Button>(docViewer, "PrintButton");
     button.Visibility = Visibility.Collapsed;
Vultuxe
  • 87
  • 9