我有一个FlowDocument,我想填充窗口的整个宽度和高度。我尝试使用FlowDocumentPageViewer
(没有运气),现在正在使用DocumentPageView
。我仍然无法停靠/填充整个空间;它只是坐在中间,它可以创造的最小尺寸(它有意义吗?)
这是我的代码:
public DocumentPageView GetPage()
{
FlowDocumentPageViewer viewer = new FlowDocumentPageViewer();
StreamReader reader = new StreamReader(location);
string data = reader.ReadToEnd();
reader.Close();
string xamlData = HtmlToXamlConverter.ConvertHtmlToXaml(data, true);
FlowDocument result = (FlowDocument)System.Windows.Markup.XamlReader.Load(new MemoryStream(System.Text.UnicodeEncoding.Default.GetBytes(xamlData)));
viewer.Document = result;
viewer.VerticalAlignment = VerticalAlignment.Center;
viewer.HorizontalAlignment = HorizontalAlignment.Center;
DocumentPageView pageView = new DocumentPageView();
pageView.VerticalAlignment = VerticalAlignment.Center;
pageView.HorizontalAlignment = HorizontalAlignment.Center;
pageView.Stretch = System.Windows.Media.Stretch.Uniform;
pageView.PageNumber = 0;
pageView.StretchDirection = StretchDirection.Both;
pageView.DocumentPaginator = ((IDocumentPaginatorSource)result).DocumentPaginator;
return pageView;
}
请注意,此代码包含我的两种方法的组合,但目前仅使用DocumentPageView
。这是从我的HTML源代码创建的Xaml:
<FlowDocument xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph TextAlignment="center" FontSize="22pt" FontFamily="arial">Test Paragraph</Paragraph>
<Paragraph TextAlignment="center" FontFamily="arial">Test second paragraph</Paragraph>
</FlowDocument>
如果我调整字体大小,则内容仅垂直调整大小(请注意,拉伸方向设置为两者)。有什么想法吗?
答案 0 :(得分:3)
我在使用FlowDocumentScrollView
时遇到了类似问题,但此解决方案似乎也适用于FlowDocumentPageView
:
FlowDocument
居中,因为它的PagePadding
属性设置为auto,auto,auto,auto
。将PagePadding设置为0
可以解决此问题。
<FlowDocumentScrollViewer VerticalScrollBarVisibility="Auto">
<FlowDocument PagePadding="0">
</FlowDocument>
</FlowDocumentScrollViewer>
答案 1 :(得分:1)
以下几行使您的元素居中(与拉伸不同):
viewer.VerticalAlignment = VerticalAlignment.Center;
viewer.HorizontalAlignment = HorizontalAlignment.Center;
pageView.VerticalAlignment = VerticalAlignment.Center;
pageView.HorizontalAlignment = HorizontalAlignment.Center;
您可以安全地删除它们,因为这里的默认对齐方式是Stretch。
如果你仍想让观众居中,请明确定义页面大小(记住,一英寸有96个点,边距和页面大小都是以磅为单位):
宽度= 96 * 8.5
高度= 96 * 11
答案 2 :(得分:0)
您能否指定使用 GetPage()方法的结果的位置和方式?是xbap还是桌面应用程序?
我问这个,因为以下文档在Kaxaml:
中完全正确显示<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<FlowDocumentPageViewer>
<FlowDocument >
<Paragraph TextAlignment="center" FontSize="22pt" FontFamily="arial">Test Paragraph</Paragraph>
<Paragraph TextAlignment="center" FontFamily="arial">Test second paragraph</Paragraph>
</FlowDocument>
</FlowDocumentPageViewer>
</Grid>
</Page>
PS:如果是桌面应用程序,您可以随时找到导致问题的人Snoop tool,形成Pete Blois。
答案 3 :(得分:-2)
更新:它是一个桌面应用程序,getpage()结果被发布到一个完美停靠/填充的网格中。
<Window x:Class="GreenWebPlayerWPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="Auto" Width="Auto" WindowStyle="None" WindowState="Maximized" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" Closing="Window_Closing">
<Grid Width="Auto" Height="Auto" Name="TransitionContainer" Background="White" Margin="0">
//here the result from GetPage() method is inserted
</Grid>
</Window>
答案 4 :(得分:-2)