如何将多个FlowDocumentReaders添加到StackPanel?

问题描述:

感谢Leom's answer我能够在FlowDocumentReader包装到一个的FlowDocument添加到的StackPanel如何将多个FlowDocumentReaders添加到StackPanel?

但现在我有两个问题:

  • 似乎只加到第一 FlowDocumentReader,其余忽略
  • 有一个不需要保证金,我无法摆脱的

如何将多个FlowDocumentReader添加到StackPanel而没有不需要的边距?

alt text http://www.deviantsart.com/upload/1ndiqqe.png

XAML:

<Window x:Class="TestFlowdoc23432.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="200" Width="300"> 
    <StackPanel Margin="10"> 
     <ContentControl x:Name="MainArea"/> 
    </StackPanel> 
</Window> 

代码背后:

using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 

namespace TestFlowdoc23432 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 

      StackPanel sp = new StackPanel(); 

      TextBlock tb1 = new TextBlock(); 
      tb1.Text = "first text block"; 
      sp.Children.Add(tb1); 

      TextBlock tb2 = new TextBlock(); 
      tb2.Text = "second text block"; 
      sp.Children.Add(tb2); 

      sp.Children.Add(GetFlowDocumentReader("first flow document reader")); 
      sp.Children.Add(GetFlowDocumentReader("second flow document reader")); 

      MainArea.Content = sp; 
     } 

     FlowDocumentReader GetFlowDocumentReader(string text) 
     { 
      FlowDocumentReader fdr = new FlowDocumentReader(); 
      FlowDocument fd = new FlowDocument(); 
      fdr.Document = fd; 
      fdr.Margin = new Thickness(0); 
      Paragraph par = new Paragraph(); 
      par.Margin = new Thickness(0); 
      fd.Blocks.Add(par); 

      Run r = new Run(text); 
      par.Inlines.Add(r); 

      return fdr; 
     } 

    } 
} 

为了使文本显示到左边,你需要设置在pagepadding财产您流程文档如下:

fd.PagePadding = new Thickness(0); 

,你似乎只得到第一个读者,原因是其填充可用空间的原因(移动它是第一个对象,你不会看到的TextBlocks)。 如果将FlowDocumentReader更改为FlowDocumentScrollViewer并使用VerticalScrollBarVisibility属性,则可以获得所需的效果。以下是应用更改的GetFlowDocumentReader方法:

FlowDocumentScrollViewer GetFlowDocumentReader(string text) 
     { 
      FlowDocumentScrollViewer fdr = new FlowDocumentScrollViewer(); 

      FlowDocument fd = new FlowDocument(); 
      fdr.Document = fd; 
      fdr.Margin = new Thickness(0); 
      Paragraph par = new Paragraph(); 
      par.Margin = new Thickness(0); 
      fd.Blocks.Add(par); 

      Run r = new Run(text); 
      par.Inlines.Add(r); 

      fd.PagePadding = new Thickness(0); 
      fdr.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden; 

      return fdr; 
     } 
+0

正确对目标,效果很好,谢谢! – 2010-02-18 12:23:15

+0

很高兴我能帮到你 – 2010-02-18 12:32:38