如何在代码中的DocumentViewer中删除/禁用打印按钮(在其自己的线程时)



我在背景线程中作为STA公寓运行以下代码,以在文档查看器中提供打印预览:

 // 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();
            }
        }

都效果很好。但是,由于这是在自己的线程中运行的 - 不是主线程 - 文档查看器上的打印对话崩溃了。

在代码中,i 如何删除和/或 disable documentViewer的打印按钮?(我已经阅读了我在Google中找到的所有内容,这一切都在XAML中,不是很有帮助)。

任何帮助都非常感谢。tia

更新#1:我能看到的唯一方法是从控制模板中删除打印按钮并使用自定义文档查看器。在文档查看器样式中给出了可行的样式。

如果我可以简单地删除系统文档查看器的按钮?

,仍然会很好。

使用此答案中的方法,您可以以这样的方式更改PrintButton的可见性。假设我将该方法放在一个名为UIElementHelper的课程中:

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

最新更新