感谢 Leom's answer,我能够通过将 FlowDocument 包装在 FlowDocumentReader 中,将 FlowDocument 添加到 StackPanel 。
但是现在我有两个问题:
如何在没有多余边距的情况下将多个 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);
您似乎只获得第一个阅读器的原因是因为它填满了可用空间(将其移动到第一个对象,您将看不到文本块)。
如果将 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;
}