2008-10-08 95 views
7

.Net包含一個很好的控件,名爲DocumentViewer。它還提供了一個用於在加載的文檔中查找文本的子控件(至少它應該做什麼)。WPF DocumentViewer查找函數和固定頁面文檔

當插入FixedPage的對象作爲DocumentViewer的文檔來源時,查找功能找不到任何內容。甚至沒有單個字母。我還沒有試過FlowDocument的呢, 爲DocumentViewer文件是沒有多大用處,並在網絡上的資源並不實際存在,我現在要問的計算器社區:

是什麼需要得到WPF DocumentViewer的查找功能與FixedPage文件一起使用?

回答

8

我在FixedDocuments中遇到了同樣的問題。如果您將FixedDocument轉換爲XPS文檔,那麼它工作正常。

從FixedDocument在內存中創建XPS文檔然後在DocumentViewer中顯示的示例。

// Add to xaml: <DocumentViewer x:Name="documentViewer" /> 
// Add project references to "ReachFramework" and "System.Printing" 
using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.IO; 
using System.IO.Packaging; 
using System.Windows.Xps.Packaging; 

namespace WpfApplication1 
{ 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      // Set up demo FixedDocument containing text to be searched 
      var fixedDocument = new FixedDocument(); 
      var pageContent = new PageContent(); 
      var fixedPage = new FixedPage(); 
      fixedPage.Children.Add(new TextBlock() { Text = "Demo document text." }); 
      pageContent.Child = fixedPage; 
      fixedDocument.Pages.Add(pageContent); 

      // Set up fresh XpsDocument 
      var stream = new MemoryStream(); 
      var uri = new Uri("pack://document.xps"); 
      var package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite); 
      PackageStore.AddPackage(uri, package); 
      var xpsDoc = new XpsDocument(package, CompressionOption.NotCompressed, uri.AbsoluteUri); 

      // Write FixedDocument to the XpsDocument 
      var docWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc); 
      docWriter.Write(fixedDocument); 

      // Display XpsDocument in DocumentViewer 
      documentViewer.Document = xpsDoc.GetFixedDocumentSequence(); 
     } 
    } 
} 

enter image description here

+0

+1完美的工作對我來說 – 2010-07-07 16:20:55

1

我曾與在RichTextBox中搜索文本麻煩[順便說一句,我不定製ControlTemplatesDocumentViewer使用],它是太慢了。我所做的就是每次我想要搜索時都要關注xaml。我改善了幾個數量級。

這是一個很大的解決方案,基於克里斯安德森的book的一部分。

乾杯