通过 Wpf-controls 打印文档并转换为 XPS [英] Print documents via Wpf-controls and convert to XPS

查看:46
本文介绍了通过 Wpf-controls 打印文档并转换为 XPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 XAML 控件转换为 XPS 文档,但我想以批处理模式执行此操作 - 在内存中呈现控件并将其打印到 XPS,而不将其呈现在屏幕上.即使没有 GUI,这个项目也应该可以工作.

I'm trying to convert XAML control to XPS document, but i'd like to do this in batch mode - render control in memory and print it to XPS without rendering it on the screen. This project should work even without GUI.

我已阅读 有关 stackoverflow 的相关主题,但它是不能正常工作.我可以创建控件,设置 DataContext,但输出 xps 为空.如果我在屏幕上呈现控件然后打印它,一切都可以,但是如果我想以批处理模式执行此操作,我会得到空文档(只有静态标签等)

I've read Related topic on stackoverflow, but it's not working properly. I can create control, set DataContext, but output xps is empty. If i render control on the screen and then print it, everything is ok, but if i'd like to do this in batch mode, i got empty document (there was only static labels etc.)

如何强制控件将控件与数据绑定?

How can i force control to bind controls with data?

下一个难点是:如果我打印长多页控件,如何在每个页面上添加自定义页眉?(例如列表?)

Next hard part would be: how can i add my custom header on each page if i print long-multi-page control? (ex. list?)

推荐答案

我将跳过您的第二个问题,因为它足够复杂,可以独立使用.

I'm skipping your second question as it is complex enough to be a standalone.

我遇到了同样的问题,但可能是由几个不同的原因引起的.

I faced the same issue, but it may be caused by a couple of different things.

如果问题是因为绑定还没有被绊倒",那么解决方案有点麻烦,但如果您控制 DataContext 类型,则很容易做到.您只需向您的类型添加一个公共或内部方法,允许您为每个公共属性触发 PropertyChanged 事件.举个例子:

If the issue is because the bindings haven't been "tripped" yet, the solution is slightly hacky but is easy to do if you control the DataContext type. You just add a public or internal method to your type that allows you to fire off PropertyChanged events for each public property. Here's an example:

public interface IForceBinding : INotifyPropertyChanged
{
  void ForceBindings();
}

public class MyDataContext : IForceBinding
{
  public event PropertyChanged;
  private string _text;
  public string Text
  {
    get{return _text;}
    set{_text = value; OnPropertyChanged("Text");}
  }
  public void ForceBindings()
  {
    OnPropertyChanged("Text");
  }

  private void OnPropertyChanged(string propertyName)
  { 
    // you know the drill
  }
}

那么,你可以这样使用它:

then, you can use it thusly:

public void Print(MyDataContext preconfiguredContext){
  var page = new MyWpfPage();
  page.DataContext = preconfiguredContext;
  preconfiguredContext.ForceBindings();
  // write to xps

如果这不起作用,您可能会遇到第一页上的绑定永远不会显示的错误.在我重新找到解决方案之前,我必须挖掘一段时间.

If that isn't working, you might be encountering a bug where the bindings on the first page never show up. I'd have to dig for awhile before I can re-find the solution to that.

这篇关于通过 Wpf-controls 打印文档并转换为 XPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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