WPF 中的文档布局有哪些选项?
使用 WPF 的 FlowDocument,我遇到了许多需要对文档布局进行更多控制的情况,从简单的内容(页眉和页脚)到更复杂的内容(脚注、杂志风格的故事流程)再到更复杂的内容(页眉和页脚)。带有批评工具的文学文本 - 我的实际要求之一)。
然而,据我所知,我唯一的选择是:
A. 使用 FlowDocument 并失去对布局的所有控制。
B. 使用 TextFormatter 从头开始编写所有内容。
A 对我来说不是一个选择,而 B 需要实现数十种方法,更重要的是,失去 FlowDocument 及其关联查看器的功能。
我的问题是:
是否有任何替代方案可以让我利用 FlowDocument 的强大功能(它满足我 90% 的布局需求),并且只编写实现其他 10% 所需的代码?
编辑: FlowDocument 的可回流焊方面对我来说至关重要。我知道我既要求可重排的内容又要求对布局的精确控制,这有些矛盾。但是,我知道这是可以完成的 - 我使用 TextFormatter 编写了一个简单的实现来完成我想要的,但我宁愿使用带有某种扩展的 FlowDocument 以避免重新实现每个功能。
编辑2:看来我真正想要的是与 FlowDocument 的内部分页器挂钩,这样我就可以向它提供布局自定义类的说明。有什么办法可以做到这一点吗?
Using WPF's FlowDocument, I have run in to a number of situations where I need more control over the layout of the document, from simple things (page headers and footers) to more complex (footnotes, magazine style story flow) to even more complex (literary texts with critical apparatus - one of my actual requirements).
However, as far as I can tell, my only options are:
A. Use FlowDocument and lose all control over layout.
B. Write everything from scratch using TextFormatter.
A is not an option for me, and B requires implementing dozens of methods, and more importantly, the loss of the power of FlowDocument and its associated Viewers.
My question is:
Is there any alternative that will allow me to leverage the power of FlowDocument, which covers 90% of my layout needs, and only write the code necessary to implement the other 10%?
EDIT: The reflowable aspect of the FlowDocument is crucial for me. I understand that I am asking for both reflowable content and precise control over layout, which are somewhat contradictory. However, I know that it can be done - I wrote a bare bones implementation using TextFormatter that accomplishes what I want, but I would MUCH rather use FlowDocument with some kind of extension to avoid reimplementing every feature.
EDIT 2: It seems that what I am really after is a hook into FlowDocument's internal paginator, so that I can give it instructions for laying out a custom class. Is there any way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WPF 中的文本系统主要设计用于处理 UI 中使用的文本,而不是用于生成带有脚注和标题等的复杂文档。但是,该框架的编写方式是,如果您想添加自定义功能,您也可以这样做。
第一个问题:脚注和与文本一致的内容。 WPF 提供了 2 个类将
UIElement
放入文本中:InlineUIContainer
和BlockUIContainer
。我会考虑编写自己的自定义控件,该控件专门设计为具有脚注或类似内容的行为,并将其放入这两个类之一。如果您需要有关什么接受什么的更多信息,我在 MSDN 上找到了这个方便的花花公子关系图表(链接位于页面底部)(来源:microsoft.com)
我'我不完全确定“杂志风格的故事流程”是什么意思。 “FlowDocument”会自动将
Block
派生类(上图中蓝色的任何内容)排列到可用空间中,并且您可以使用Floater
和Figure
内联元素。您还可以使用Figure
和Floater
作为页眉和页脚功能。以下是一些示例代码:
您可以将
Button
替换为您自己的自定义控件(例如,带有脚注的内联按钮)此代码如下:
我希望有帮助!我不知道你到底想做什么,但我认为你仍然可以使用 FlowDocument 并只使用 WPF 提供的大量文本操作设备,如果你确实需要额外的功能/布局选项创建一个继承
Block
或Inline
或其他内容的新类,并在其中编写额外的内容,以利用 .net 可以为您完成的所有工作。如果您需要更多信息,可以在 MSDN 上阅读有关 WPF 中文本内容的更多信息:
超长文章关于如何使用 FlowDocument
WPF 中使用的文本内容模型(我从其中获取图像)
尽情享受吧:)
The text system in WPF is primarily designed for playing around with text for use in UIs, not for producing complex documents with footnotes and headers etc. However, the framework has been written so that if you want to add custom functionality, you can.
First problem: Footnotes and stuff that is in-line with the text. WPF provides 2 classes to put
UIElement
s in text:InlineUIContainer
andBlockUIContainer
. I would consider writing your own custom control that is specially designed to have the behaviour of a footnote or something similar and put it in one of those two classes. I found this handy-dandy relationship chart on MSDN if you need more info on what accepts what (links are at the bottom of the page)(source: microsoft.com)
I’m not completely sure what you mean by “magazine style story flow”. 'FlowDocument' will automatically arrange
Block
-derived classes (anything in blue on the above chart) into the available space and you can make the text ‘flow’ around objects using theFloater
andFigure
inline elements. You could also useFigure
andFloater
for your headers and footers feature.Here is some example code:
You can replace the
Button
s with your own custom controls (eg. the inline button with your footnote thang)This code makes this:
I hope that helps! I don't know exactly what you are trying to do but I would think that you could still use
FlowDocument
and just use the large amount of text manipulating equipment provided with WPF and if you do need extra functionality/ layout options create a new class inheritingBlock
orInline
or whatever and write the extra stuff in there to take advantage of all the work .net can do for you.If you need more information you can read more about text stuff in WPF on MSDN:
Extra long article about how to use FlowDocument
The text content model used in WPF (where I got the image from)
Enjoy yourself :)
答案实际上很简单:FixedDocument
现在,使用FixedDocument,您将失去FlowDocument 的屏幕灵活性,但您将获得对几乎所有内容的支持,并且DocumentViewer 是固定文档的绝佳查看器。
此外,您还可以将固定文档保存到 XPS 并在应用程序外部查看它们。
此代码展示了如何获取 FLowDocument 并将其转换为带有页眉、页脚和边距的固定文档。我认为调整此代码以支持脚注应该不会太困难。
The answer is actually simple: FixedDocument
Now, with FixedDocument you will lose the on-screen flexibility of the FlowDocument but you will gain support for just about everything and DocumentViewer is a great viewer for fixed documents.
Also, you can save Fixed documents to XPS and view them outside you app.
This code shows how to take a FLowDocument and convert it to a FixedDocument with headers, footers and margin. I think it shouldn't be too difficult to adapt this code to also support footnotes.
我们使用 Apache FOP(一种开源 XSL-FO 实现)来创建这些类型的文档。它实际上是用 Java 编写的,但我们使用 IKVM 在 .NET 上运行它。 IKVM 是在 .NET 上运行的 Java 开源实现。它工作得非常好。 FOP 生成 PDF、RTF 和其他几种格式。
缺点是您需要学习 XSL-FO,但如果您习惯了老式 HTML,那么这并不困难。
http://xmlgraphics.apache.org/fop/
http://www.ikvm.net/
http://www.ikvm.net/uses.html
http ://www.w3schools.com/xslfo/default.asp
We use Apache FOP, an open source XSL-FO implementation, to create these types of documents. It's actually written in Java but we use IKVM to run it on .NET. IKVM is an open source implementation of Java that runs on .NET. It works pretty nicely. FOP produces PDF, RTF, and several other formats.
The downside is you'll need to learn XSL-FO, but it's not difficult if you're used to old school HTML.
http://xmlgraphics.apache.org/fop/
http://www.ikvm.net/
http://www.ikvm.net/uses.html
http://www.w3schools.com/xslfo/default.asp