打印与WPF中的动态数据的FlowDocument [英] Printing a flowdocument with dynamic data in WPF

查看:2881
本文介绍了打印与WPF中的动态数据的FlowDocument的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到打印在WPF流文档的好方法。我想是有可能怎么看文件原来是我设计的,因此,因此营造一个纯粹的FlowDocument为XAML是出了问题(如Visual Studio中不会显示在设计视图的话)。

I am trying to find a good way to print a flow document in WPF. What I want is to have a possibility to see how the document turns out as I design it, so therefore creating a pure FlowDocument as a XAML is out of the questions (as Visual Studio wont show the design view for it).

那么,我现在所做的就是创建一个包含的FlowDocument这样的(有些过度的部分已被删除,使code更consise)的窗口:

So what I have done now is to create a window that contains a FlowDocument like this (some excessive parts have been removed to make the code more consise):

<Window x:Class="MyNamespace.ProjectPrintout...>
  <Grid>
    <FlowDocumentReader>
      <FlowDocument ColumnWidth="500" Name="Document">
        <!-- Header -->
        <Paragraph Name="HeaderText">
          The header will go here
        </Paragraph>
      </FlowDocument>
    </FlowDocumentReader>
  </Grid>
</Window>

这是一个有点奇怪,因为我永远都不会显示这个窗口给用户,而我只换了FlowDocument的一个窗口,这样我可以看到它的样子,因为我开发它。这活的人生指明使用。

This is a bit strange since I will never show this Window to the user, and I only wrap the FlowDocument with a Window so that I can see how it looks like as I develop it. This Ican live with.

所以别人在我的应用程序的某个地方,我需要打印的FlowDocument到默认打印机,但我也必须动态地设置页眉(除了需要动态的数据在这里被省略了文件的许多其他部分)。

So somewhere else in my application, I want to print this FlowDocument to the default printer, but I also have to set the header dynamically (in addition to many other parts of the documents that needs dynamic data that are omitted here).

在code打印看起来是这样的:

The code to print looks like this:

  var printout = new ProjectPrintout();
  printout.HeaderText= new Paragraph(new Run("Proper header text"));
  var document = printout.Document;

  var pd = new PrintDialog();
  IDocumentPaginatorSource dps = document;
  pd.PrintDocument(dps.DocumentPaginator, "Document");

该文件得到打印的,看起来正常,只是此时的标题文字仍显示头会在这里,即使我从我的code以适当的标题文字取而代之。我也试图改变这样的:

The document is getting printed, and looks fine except that the header text still shows "The header will go here", even if I replaced it from my code with "Proper header text". I also tried changing it like this:

(printout.HeaderText.Inlines.FirstInline as Run).Text = "Proper header text";

但结果是相同的。

But the result is the same.

所以,问题是:我如何可以更改的FlowDocument的内容从code之前,我打印出来,或者有更好的方法做的,而不是我的方法这个

So the question is: How can I change the contents in the FlowDocument from code before I print it, or are there a better way to do this instead of my approach?

推荐答案

MVVM 救援:

顿悟:用户界面是不是数据。用户界面是不是一个数据存储。 UI是为了显示数据,而不是存储它。

1 - 创建一个简单的对象来保存你的数据。

1 - Create a simple object to hold your data

public class MyDocumentViewModel: INotifyPropertyChanged //Or whatever viewmodel base class
{
    private string _header;
    public string Header 
    {
        get { return _header; }
        set
        {
            _header = value;
            NotifyPropertyChange(() => Header);
         }
     }

     //Whatever other data you need
}

2 - 定义绑定 S在您的文档;

2 - Define Bindings in your Document;

<Paragraph>
    <Run Text="{Binding Header}"/>
</Paragraph>

3 - 设置你的FlowDocument的的DataContext 这个类的一个实例:

3 - Set your FlowDocument's DataContext to an instance of this class:

var flowdoc = new YourFlowDocument();
var data = new MyDocumentViewModel { Header = "this is the Header" };
//whatever other data

flowdoc.DataContext = data;

//do the printing stuff.

这篇关于打印与WPF中的动态数据的FlowDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆