2012-07-09 60 views
4

previous question我試圖從我的TreeView打印,現在我去到父視圖,我有一個UserControl如何包含一個TreeView來顯示我的WPFview我的搜索結果,我試圖打印它和i相FlowDocumentScrollViewer包圍作爲代碼如下:用FlowDocumentScrollViewer打印AllPages

view.xaml

<FlowDocumentScrollViewer x:Name="flow" Margin="10" BorderThickness="0"> 
<FlowDocument> 
    <Section> 
     <BlockUIContainer> 
      <my:SearchTreeView Content="{Binding Stvm}"/> 
     </BlockUIContainer> 
    </Section> 
</FlowDocument> 

view.xaml.cs

Printing.PrintDoc(flow.Document, txtSearch.Text + " - Result"); 

靜態printing.cs

public static void PrintDoc(System.Windows.Documents.FlowDocument fd, string description) 
    { 
     double h, w, cw;    
     h = fd.PageHeight ; 
     w = fd.PageWidth ; 
     cw = fd.ColumnWidth; 

     PrintDialog pd = new PrintDialog(); 
     //pd.PageRangeSelection = PageRangeSelection.AllPages; 
     //pd.UserPageRangeEnabled = true; 

     if (pd.ShowDialog() != true || fd == null) return; 

     fd.PageHeight = pd.PrintableAreaHeight; 
     fd.PageWidth = pd.PrintableAreaWidth; 
     fd.PagePadding = new Thickness(50); 
     fd.ColumnGap = 0; 
     fd.ColumnWidth = pd.PrintableAreaWidth; 

     System.Windows.Documents.IDocumentPaginatorSource dps = fd; 
     // int c = dps.DocumentPaginator.PageCount;//0 

     pd.PrintDocument(dps.DocumentPaginator, description); 

     fd.PageHeight = h; 
     fd.PageWidth = w; 
     fd.PagePadding = new Thickness(0); 
     fd.ColumnWidth = cw; 
    } 

我tryed幾乎所有的exmple在類似的問題,但我得到了最好的僅僅是第一頁結果像this ..; (

我使用WPF MVVM模式。這是否是正確的控制措施?或者我應該去做任何其他WPF控件。?

+1

我在這裏回答了類似的問題。 http://stackoverflow.com/questions/6083555/itemscontrol-how-to-print-wrappanel-on-multiple-pages/18560535#18560535 感謝 – Nitin 2013-09-01 17:18:27

回答

0

我解決這個問題的方法是通過在我的視圖的* .xaml部分中定義一個包含空表格對象的FlowDocument控件。然後,我通過代碼隱藏(即視圖的* .xaml.cs部分)動態地將包含我想要打印的控件的塊添加到FlowDocument中的Table中。如果您定義要在單獨的塊中打印的每個UI元素,則表格會在打印時自動執行分頁。您需要找出一種方法將SearchTreeView控件分解爲更小的控件,以便它不僅包含在一個Block中。

有關Table控件的詳細信息請看這裏:http://msdn.microsoft.com/en-us/library/ms747133%28v=vs.110%29.aspx