Flowdocument 未使用完整宽度/高度

发布于 2024-08-03 22:04:55 字数 1827 浏览 2 评论 0原文

我有一个 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>

如果我调整字体大小,则内容只会垂直调整大小(请注意,拉伸方向设置为两个方向)。有什么想法吗?

I have a FlowDocument which I want to fill the entire width and height of my window. I have tried using the FlowDocumentPageViewer (no luck) and am now using a DocumentPageView. I still can't get it to dock/fill the entire space; it's just sitting in the middle, in the minimum size it can create (does it make sense?)

Here is my code:

   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;
   }

Please note that this code contains the combination of my two methods but only the DocumentPageView is currently used. This is the Xaml that is created from my HTML source:

<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>

If I resize the fonts the content is only resized vertically (please note that the stretch direction is set to both). Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

奢欲 2024-08-10 22:04:55

我在 FlowDocumentScrollView 上遇到了类似的问题,但此解决方案似乎也适用于 FlowDocumentPageView

FlowDocument 居中,因为它是 PagePadding< /code> 属性设置为 auto,auto,auto,auto。将 PagePadding 设置为 0 可修复此行为。

<FlowDocumentScrollViewer VerticalScrollBarVisibility="Auto">
    <FlowDocument PagePadding="0">
    </FlowDocument>
</FlowDocumentScrollViewer>

I had a similar problem with FlowDocumentScrollView, but this solution also seems to work with FlowDocumentPageView:

The FlowDocument is centered because it's PagePadding property is set to auto,auto,auto,auto. Setting PagePadding to 0 fixes this behavior.

<FlowDocumentScrollViewer VerticalScrollBarVisibility="Auto">
    <FlowDocument PagePadding="0">
    </FlowDocument>
</FlowDocumentScrollViewer>
德意的啸 2024-08-10 22:04:55

以下几行导致元素居中(这与拉伸不同):

viewer.VerticalAlignment = VerticalAlignment.Center;
viewer.HorizontalAlignment = HorizontalAlignment.Center;
pageView.VerticalAlignment = VerticalAlignment.Center;
pageView.HorizontalAlignment = HorizontalAlignment.Center;

您可以安全地删除它们,因为此处的默认对齐方式是拉伸。

如果您仍想让查看器居中,请显式定义页面大小(请记住,一英寸有 96 点,边距和页面大小以点为单位设置):

宽度= 96 * 8.5

高度= 96 * 11

The following lines are causing your elements to center themselves (which is not the same as stretch):

viewer.VerticalAlignment = VerticalAlignment.Center;
viewer.HorizontalAlignment = HorizontalAlignment.Center;
pageView.VerticalAlignment = VerticalAlignment.Center;
pageView.HorizontalAlignment = HorizontalAlignment.Center;

You can safely remove them, as the default alignment here is Stretch.

If you still want to center the viewer, explicitly define the page size (remember, there are 96 points in an inch, and the Margin and PageSize are set in points):

Width= 96 * 8.5

Height= 96 * 11

寄居者 2024-08-10 22:04:55

您能否指定在何处以及如何使用 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:如果它是在桌面应用程序中,您始终可以通过 Pete Blois 的 Snoop 工具 找到导致问题的人。

Could you specify where and how do you use the result of your GetPage() method? Is it xbap or desktop application?

I'm asking this, because the following document is displayed just perfectly right in 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: If it's a desktop application you can always find who causes problems by Snoop tool, form Pete Blois.

吃不饱 2024-08-10 22:04:55

更新:它是一个桌面应用程序, 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>

Update: Its a desktop application, the getpage() result is posted into a grid which docks/fills perfectly.

<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>
海螺姑娘 2024-08-10 22:04:55

(此评论是从另一个帐户写的)

@Anvaka:我所说的完全正确的意思是文档应该“停靠在容器中”,即字体应该调整大小,它应该填充容器的高度和宽度。现在我想起来了,这对于流程文档来说似乎不是一个正确的行为。

当我将流文档放入容器中时,它位于父容器的中间(到目前为止一切顺利)。但父容器没有填充其父容器,因此当我缩放或更改字体大小时,我希望 documentPageView 容器的宽度和高度增加,但保持居中。

(this comment is written from another account)

@Anvaka: What i mean by perfectly right is that the document should "dock in container" that is the fonts should resize, it should fill the container in height and width. Now that im thinking about it, that may not seem like a proper behaviour for a flowdocument.

When i put the flow document in the container it is centered in middle of parent container (so far so good). but the parent container is not filling out its parent container so when i zoom or change font size, i would like the documentPageView container to grow in width and height, but remain centered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文